Python初识+条件语句+循环语句
一、写照:
1.第一个程序 hello world
print('hello world')
后缀名可以是任意(只是现在)(lx.py lx.ps)
导入模块时不是.py 就会出错
2.解释器路径
#!usr/bin/env python
3.编码
#-*- coding:utf8 -*-
ascill 00000000
& 00000001
unicode 0000000000000000+
& 0000000000000001
utf-8 能用多少表示就用多少
& 00000001
4.执行一个操作(用户验证)
(单行注释# 多行 用“””)
二、变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
- 最好不要和Python内置东西重复
a1_b2="敏敏真漂亮" print(a1_b2)
单词与单词之间一般用下划线。
三、条件语句
if 1==1: print('欢迎进入我的世界') else: print('Error')
一定注意空格 一般为4个
if 1==1: if 2==3: print('欢迎进入我的世界') print('欢迎进入我的世界1') else: print('1111111') else: print('Error')
if elif 运用:
inp=input('请输入用户名:') if inp=="高级会员": print('美女') elif inp=="白金会员": print('一线明星') else: print('城管') print('开始服务吧......')
pass 运用:
if 1==1: pass else: print('2')
输出的是空白!
四、数据类型
1.字符串(引号四种) 引号里面的叫字符
name="我是刘翔" name='我是刘翔' name="""我是刘翔""" name='''我是刘翔'''
加法:(字母)
n1="sd"
n2="gg"
n=n1+n2 (sdgg)
n1="sd" n2="gg" n3=n1+n2 print(n3)
特别注意这里的print 里面不能加引号
乘法:n3=n1*10 (结果是输出10次sd sdsdsdsdsd.......)
没有 除法和减法
2.数字
加减乘除都有 以及2**4(2的4次方) 39%8 (39/8得到的余数 7) 39//8 (取的是商 4)
a=5 lx=a%2 if lx==0: print('偶数') else: print('奇数')
num=int(input('请输入一个数字:')) if num%2==0: print('偶数') else: print('奇数')
注意这个int 表示整数型字符 不加int 表示的是字符串
五、循环
无限循环:
while 1==1: print('ok')
习题1:使用while循环 输出1 2 3 4 5 6 8 9 10
i=1 while i<101: temp=i%2 if temp==0: pass else: print(i) i = i + 1
习题2:输出1-100的所有奇数
i=1 while i<101: temp=i%2 if temp==0: pass else: print(i) i = i + 1
习题3:求1-100所有数的和
i=1 n=0 while i<101: n = n + i i = i + 1 print(n)
习题4:求1-2+3-4.......99的总和
i=1 n=0 while i<100: temp=i%2 if temp==0: n = n - i else: n = n + i i = i + 1 print(n)