一、变量 # print("hello world") # age=18 # #定义一个变量,会有2个特征:id,type,value # print(id(age),type(age),age)(查看变量的类型) # name="egon" # print(id(name),type(name),name) #变量的命名方式: # 1.驼峰体 # AgeOfolaboy=73 # 2下划线 # age_of_oldboy=73 #常量(大写方式) # AGE_OF_OLDBOY=73 # #AGE_OF_OLDBOY=71 # print(AGE_OF_OLDBOY) 二、用户与程序交互
#!/usr/bin/env python #coding:utf-8(python2解决raw_input报错,在文档第一行写coding:utf-8) #在Python3中,无论输入何种类型,都会存成字符串类型 # name=input('please input your name:') # print(id(name),type(name),name) #在python2中,raw_input与Python3的input一样 # name=raw_input('please input your name:') # print (id(name),type(name),name) #python2中input,用户必须输入值,输入的是什么类型,就存成什么类型 # name=input('please input your name:') # print (id(name), type(name), name) 三、基本数据类 #数字类 #int整型类型:身份证号,QQ号,等级等;只能存一个值,没有顺序,不可变 #age=18 #age=int(18)
#float浮点型:身高,体重,薪资等;存一个值,没有顺序,不可变 #height=1.81 # height=float(1.81) # print(type(height),height) #字符串类型:把一堆字符放到单引号或者双引号或者三引号中 #表示一些描述性的状态,比如人的名字,性别 # name="egon"#(注意用英文字符) # print(type(name)) # # comment=''' # 老男孩的Alex,买了一辆车,然而。。。 # ''' #msg="i'm ok" #字符串拼接: #1只能字符串之间拼接 #2字符串之间只能用+或者* # name='egon' # msg='hello' # age=18 # print(name+msg+str(age)) # print(name*10) #列表:定义在【】内,用逗号分隔开的多个元素,每个元素可以是任意类型 #用来表示存取多个值,比如人的爱好,人的信息 # hobbies=['play', 'read','music','movie'] # print(hobbies [0]) # print(hobbies [3]) # l=[1,1.3,'ha',['a','b']] # print(l[3][1]) # id name sex hobbies # info=[1311221994,'ling','male',['music','read']] # print(info[1]) #字典:定义在{}内,用key=value形式表示一个元素,用逗号隔开 # info={'id':1311221994,'name':'ling','sex':'male','hobbies':['music','read']} # print(info['name']) # print(info['hobbies'][1]) #布尔类型 #print(type(True)) # AGE=72 # age=18 # print(age>AGE) # print(age
#bool值:所有的数据类型都自带bool值 #bool值为假的情况:1、对于数字来说0的时候为假。2、空列表、空集合、空字符串。。。。为假3、none x=1 print(bool(x)) if x: print('ok')
四、格式化输出
#my name is ***,my age is *** # name=input('user_name:') # age=input('user_age:') #print('my name is %s,my age is %s'%(name,age))相当于print('my name is %s,my age is %s'%('ling','23')) #print('my name is %s,my age is %s'%('ling',23))%s占位符可以接收字符串str与整型的int print('my name is %s,my age is %d'%('ling',23))%d占位符只可以接收整型的int
五、基本运算符
# a=10 # b=3 # print(a+b) #print(a/b)#真正的除法,有整数有余数 #print(a//b)#地板除,只有整数 # print(a%b) # print(a*b) #比较运算 #age=23 # print(age>70) # print(age<70) # print(age!=70) # print(age==70) #赋值运算 # height=180 # height+=1#height=height+1 # print(height) #逻辑运算 # name='ling' # age=23 # print(name=='ling1' and age>20) # print(name=='ling2' or age>20) age=11 print(not age>5)
六、流程控制之if...else语句
# age=input('输入年龄:') # age=int(age) # if age>30: # print('叫阿姨') # else: # print('叫美女') # sex=input('sex>>:') # age=int(input('age>>:')) # is_pretty = True # if sex == 'female'and age>18 and age<30 and is_pretty == True: # print('表白中') # else: # print('叫阿姨') # sex=input('sex>>:') # age=int(input('age>>:')) # is_pretty = bool(input('is pretty:')) # if sex == 'female'and age>18 and age<30 and is_pretty == True: # print('表白中') # else: # print('叫阿姨') #if嵌套 # sex=input('sex>>:') # age=int(input('age>>:')) # is_pretty = bool(input('is pretty:')) # success=True#(满足条件1的情况下,打印在一起)False(满足条件1的情况下,打印去你妈的爱情) # if sex == 'female'and age>18 and age<30 and is_pretty == True:#(条件1) # if success: # print('在一起') # else: # print('去你妈的爱情') # else: # print('叫阿姨') #if多分支 # score=int(input('your score>>:')) # if score>=90: # print('优秀') # elif score>=80: # print('良好') # elif score>=70: # print('及格') # else: # print('太差') # name=input('please input your name:') # password=input('please input your password:') # if name=='ling' and password=='123': # print('登陆成功') # else: # print('用户名或密码错误') # ''' # ling -管理员 # mei - 职员 # zhang,liu- 主管 # 其他 - 普通用户 # ''' # name=input('输入用户名:') # if name=='ling': # print('管理员') # elif name=='mei': # print('职员') # elif name=='zhang'or name=='liu': # print('主管') # else: # print('普通用户') # today=input('>>:') # if today in ['Monday','Tuesday','Wednesday','Thursday','Friday']: # print('上班') # elif today in ['Saturday','Sunday']: # print('出去浪') # else: # print('''必须输入其中一种: # Monday # Tuesday # Wednesday # Thursday # Friday # Saturday # Sunday''')
七、流程控制之while循环
# AGE_OF_OLDBOY=73 # guess=int(input('>>:')) # if guess > AGE_OF_OLDBOY: # print('太大了,往小里猜') # elif guess < AGE_OF_OLDBOY: # print('太小了,往大里猜') # else: # print('恭喜你,猜对了') #while:条件循环 # while 条件: # 循环体 # count=0 # while count<3: # print('loop',count) # count+=1 #break跳出本层循环(打印0-100) # count=0 # while True: # if count>100: # break # print(count) # count+=1 # #continue:跳出本次循环(打印1-10,7不打印) # count=0 # while count <= 10: # if count == 7: # count+=1 # continue # print(count) # count+=1 #while+else # count=0 # while count <=10: # if count==3: # break # print(count) # count+=1 # else: # print('while正常结束了,没有被break打断,才会执行这里') #注意一 # count=0 # while count <=5: # print(count) # count+=1 # #continue加到这里没有意义 #注意二 #用户登陆,输入正确用户密码可直接登陆,输入错误3次则退出 # name='ling' # password='123' # count=0 # while count<3: # u=input('输入用户名:') # p=input('输入登陆密码:') # if u == name and p == password: # print('登陆成功') # break # else: # print('用户名或密码错误') # count+=1 #同上 name='ling' password='123' count=0 while True: if count == 3: break u=input('输入用户名:') p=input('输入登陆密码:') if u == name and p == password: print('登陆成功') break else: print('用户名或密码错误') count+=1 #登陆后可以操作命令 # name='ling' # password='123' # count=0 # while True: # if count == 3: # break # u=input('输入用户名:') # p=input('输入登陆密码:') # if u == name and p == password: # print('登陆成功') # while True: # cmd=input('>>:') # if cmd == 'quit': # break # print('run%s'%cmd) # break # else: # print('用户名或密码错误') # count+=1 #同上 # name='ling' # password='123' # count=0 # tag=True # while tag: # if count == 3: # break # u=input('输入用户名:') # p=input('输入登陆密码:') # if u == name and p == password: # print('登陆成功') # while tag: # cmd=input('>>:') # if cmd == 'quit': # tag=False # continue # print('run%s'%cmd) # # else: # print('用户名或密码错误') # count+=1