Python 3.5.4 (v3.5.4:3f56838976, Aug 7 2017, 12:56:33) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print('hello world') hello world >>> exit()
1. 交互式运行环境备注---说明
2.交互式环境使用场景---问题
3.脚本方式运行代码
练习:
print('Hello World'+'+'+ str(2)) print(2*3.1415926*10) print(3.1415926*10*10) print('100+2=',100 +2) print('1-5 = ',1-5) print('1*5 = ',1*5) print('1/5 = ',1/5) -》结果输出 Hello World+2 62.831852 314.15926 100+2= 102 1-5 = -4 1*5 = 5 1/5 = 0.2
变量定义&赋值
a_pi = 3.1415926 ra = 10 print (2*a_pi*ra) print(a_pi*ra*ra) 输出结果: 62.831852 314.15926
定义变量
pi=3.1415926 area = pi * radius ** 2
注:变量命名规则:
整数、浮点数、正数、负数
像年龄、身高、体重、分数、圆周率这样的数字 height = 1.71 age =29 wiht =140 pi = 3.1415926 score = 5.5
数据类型的运算---四则运算
除(/)
print(type(1)) print(type(1.0)) print(type(''))
type结果:
<class 'int'>
<class 'float'>
<class 'str'>
* 类型相互转化 - int/str => float - float/str => int - int/float => str ```python print(type(int(1.9))) print(type(int(2))) print(type(float(1))) print(type(str(1))) print(type(str(1.8))) 类型输出结果 <class 'int'> <class 'int'> <class 'float'> <class 'str'> <class 'str'>
字符串类型
使用单引号、双引号、三个单引号或三个双引号引起来的一些字符
name = 'dxy'
job = "linux"
特殊字符
\ 转义符
\r 回车
\n 换行
\t tab键
\f 换页
print("i 'm dxy") print('i\'m dxy') print('a \nb \tc ') print('a\\nb\\tc\\')
练习 name = str('dxy') age = int('20') input('please name and age->:') print('My name is',name,'Im,',age,'years old')
提示用户从控制台输入一个分数
#妻子的想法 momeny = 100 prompt = input('看到卖西瓜的了吗?(Y/N)') if prompt =='Y': print('买一斤包子需要花费:10元') momeny -= 10 if prompt =='Y': print('买一个西瓜需要花费:20元') momeny -= 20 print('剩余金额'+ str(momeny)) #老公想法 momeny =100 prompt1 = input('看到卖西瓜的了吗?(Y/N)') if prompt1 == 'Y': print('买一个包子需要:3元') momeny -= 3 else: print('买一斤包子需要:10元') momeny -= 10 print('剩余金额'+str(momeny))
根据表达式的真假控制代码的是否结束子语句循环执行,如果为真则继续循环执行
total = 0 idx = 1 while idx <= 100: total = total+ idx idx = idx+1
print(total)
练习 1. 循环提示用户在控制台上输入数字或者exit,当用户输入exit后结束程序,并打印所有输入数字的和与平均数 ```python total = 0 count = 0 input_number = '' while input_number !='exit': input_number = input('请输入一个数字--:') if input_number != 'exit': total += float(input_number) count += 1 if count !=0: print('total',total,'avg',total/count) else: print('total:', total, ', avg:', 0)
break 跳出循环
continue 跳过本次循环,继续下一次循环条件判断
idx = 0 while idx <= 10: idx += 1 if idx == 4: continue else: if idx ==9: break print(idx)
nums = [1, 5, 6, 3, 2, 5] for nums1 in nums: print(nums1)
作业
打印乘法口诀
提示:尝试print(‘kk’)与print(‘kk’, end=‘’)的区别
x = 0 while x <9: x += 1 # print(x) y=0 # print(y) while y < x: y += 1 print("%d*%d=%2d" % (x,y,x*y),end=" ") print('\n')
import random num_random = random.randint(0,100) count = 1 while True: input_num = int(input('游戏限制输入5次结束,请慎重输入>>')) if input_num ==num_random: print('高手!猜对了') break elif input_num > num_random: print('猜大了!!小伙伴') else: print('猜小了!!小伙伴') count =count+1 if count > 2: print('太笨了,下次再来,正确的数字是',int(num_random)) break
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。