一:python安装及开发工具(pycharm)安装
1.在python官网(https://www.python.org/)安装最新版的python文件。
2.下载pycharm最新版并安装(www.jetbrains.com)
二:第一个python程序(Hello World)
print(‘hello World‘)
三:变量
1.变量声明:
1 name = ‘jone‘ 2 name2="jone"#字符变量要用‘xx‘或者"xx" 3 num = 1
2.输出变量:
1 print(name,name2) 2 #或者print(name+name2)
四:用户交互式输入
1 name = input(‘name:‘) 2 print("Hello",name)
显示name:提示用户输入数据,假设为‘jone’将jone赋值给name这个变量
五:格式化输出
1.
1 name = ‘jone‘ 2 age = ‘18‘ 3 address = ‘shandong‘ 4 info =‘‘‘ 5 ------‘‘‘+name+‘‘‘------- 6 name:‘‘‘+name+‘‘‘ 7 age:‘‘‘+age+‘‘‘ 8 address:‘‘‘+address+‘‘‘ 9 ‘‘‘ 10 print(info)
不推荐使用该方法且只能食醋出字符串,不能输出int型
2.
1 name = ‘jone‘ 2 age = 18 3 address = ‘shandong‘ 4 info =‘‘‘ 5 ------%s------- 6 name:%s 7 age:%d 8 address:%s 9 ‘‘‘%(name,name,age,address) 10 print(info)
每一个小括号里的值对应一个格式输出的值
3.
1 name = ‘jone‘ 2 age = 18 3 address = ‘shandong‘ 4 info =‘‘‘ 5 ------{name1}------- 6 name:{name1} 7 age:{age1} 8 address:{address1} 9 ‘‘‘.format(name1 = name, 10 age1 = age, 11 address1 = address) 12 print(info)
4.
name = ‘jone‘ age = 18 address = ‘shandong‘ info =‘‘‘ ------{0}------- name:{0} age:{1} address:{2} ‘‘‘.format(name, age, address) print(info)
六:if...语句
1 #猜年龄 2 age_of_me = 20 3 age =int(input(‘guess age:‘)) 4 if age == age_of_me: 5 print(‘get it‘) 6 elif age > age_of_me: 7 print(‘too old‘) 8 else:print(‘too young‘)
七:while循环
1 #猜年龄 2 age_of_me = 20 3 i = 0 4 while i<3: 5 age = int(input(‘guess age:‘)) 6 if age == age_of_me: 7 print(‘get it‘) 8 break 9 elif age > age_of_me: 10 print(‘too old‘) 11 i += 1 12 continue 13 else: 14 print(‘too young‘) 15 i += 1 16 else: 17 print(‘game over‘) #只有循环正常结束才会运行,被berak中断的不会执行
八:for循环:
1.
for i in range(10): print(i)
补充:range函数range(1,2,3) * range函数是一个用来创建算数级数序列的通用函数,返回一个[start, start + step, start + 2 * step, ...]结构的整
数序列
1.开始值,缺省默认为0
2.结束值,不可缺省
3.每个x个数返回一个
2.猜年龄
1 #猜年龄 2 age_of_me = 20 3 i = 0 4 for i in range(3): 5 age = int(input(‘guess age:‘)) 6 if age == age_of_me: 7 print(‘get it‘) 8 break 9 elif age > age_of_me: 10 print(‘too old‘) 11 i += 1 12 continue 13 else: 14 print(‘too young‘) 15 i += 1 16 else: 17 print(‘game over‘) #只有循环正常结束才会运行,被berak中断的不会执行
原文地址:https://www.cnblogs.com/fthpython/p/8505322.html
时间: 2024-09-30 17:52:10