一、python的基础
1、简单写一行python代码:
print(‘hello,world’)print(‘你好,北京’)
2、变量:
name="刘梦雅"print(name)
程序交互:
name=input“请输入你的名字”print(name)
3、数据类型
(1)int,long类型:
a=2*20print(a,type(a))
(2)字符串类型str:
name=input('请输入你的名字')print(name,type(name))()
(3)布尔类型:
print(true,type(true))
4、if语句
choice=input(‘请输入你猜的数字’)if choice==‘2’ print(‘我请你吃饭’)else print(‘你请我吃饭’)
5、while语句
flag = true;count =1;while flag: print(count);
6、break语句:
count=1while true: print(count) count=count+1 if count==101 break
7、continue语句
count=0 while count<10; count =count +1if count ==7;continue ;print(count )
8、in not in的用法:
comment =input(‘请输入你的评论’) if ‘傻子’ in comment: print (‘您输入的为敏感词汇,请重新输入’)
9、while else
count = 1while True: print (count ) if count == 3:break count +=1else print('循环正常')
输出 1
2
3
count =1flag = Truewhile flag : print (count) if count == 3: flag=False count+=1else : print ('循环正常完毕')
输出:1
2
3
循环正常完毕
10、格式化输出:
1)第一种
name=input(‘请输入你的名字:’)age =input(‘请输入你的年龄:’)hobby=input(‘请输入你的爱好:’)ms =‘我叫%s,今年%d,爱好是%s’ % (name,int(age),hobby)print(ms)
2)第二种
dic = {‘name’:'oldboy','age':15,'hobby':'踢足球'}msg=‘我叫%(name)s,今年%(age)d,爱好是%(hobby)s’ % dicprint(msg)
3)第三种
再格式化输出中单纯的显示% 用%%解决
name=input(‘请输入你的名字:’)age =input(‘请输入你的年龄:’)msg=‘我叫%s,今年%d,学习进度为1%%’ % (name,int(age))print(msg)
11、运算符
print(int(True))print (int(False)) 输出:1 0 Trueprint(bool(100))
and or not
print (1 or 3) 输出 1print (1 and 3 ) 3print (0 or 2) 2print(0 and 2) 0print (1>2 or 3 and 4) 4