今天是python学习的第一天,收获还是不少的,使用的编辑器为python3.7。
第一天学习知识总结:
1、编写的第一句python语句:
print ( " hello world" )
2、python的两种执行方式
--python解释器 py文件路径
--python进入解释器:
实时输入并获取到执行结果
3、python的解释路径
#!/user/bin/env python
4、input 的用法
--永远等待,直到用户输入了值,就会将输入的值赋值给一个变量
例如:
n1 = input ('请输入用户名')
n2 = input ('请输入密码')
print (n1)
print(n2)
print('....')
5、变量名
组成: 字母 数字 下划线
Ps:
数字不能开头
不能是关键字
最好不要和python内置的内容重复
6、条件语句
if基本语句
1、if 条件 :
代码块
else :
代码块
例如:if 1==1 :
print ( "Welcome")
else :
print ( "out ")
2、if 支持嵌套
if 1==1 :
if 2==2 :
print ('欢迎进入第一会所')
else :
print ('error')
else :
print ('欢迎进入第一道所')
3、if elif
inp = input ('请输入会员等级')
if inp == "高级会员" :
print ('美女')
elif inp == "白金会员" :
print ('达摩')
elif inp == "铂金会员" :
print ('一线小明星')
else :
print ('城管')
print ('开始服务吧')
补充:pass(条件成立,不执行任何语句)
if 1==1 :
pass
else :
print ('输入错误')
7、字符串
用引号引起来的内容:单引号 双引号 三引号引起的内容,谁开头谁结尾,不能混用
加法:将两个字符串拼接成一个字符串,并赋予一个新的变量
乘法:让字符串重复出现
name = "alex"
na = name * 2
n = name + na
print (n)
print (name)
print (na)
数字:
加法
减法
乘法
除法
次方
余数
a1 = 10
a2 = 20
a3 = a1 + a2
a4 = a2 - a1
a5 = a1* a2
a6 = a1 / a2
a7 = a1 ** a2
a8 = a1 % a2 #获取余数,来判断奇偶数
a9 = a1 // a2 #商取整
8、循环
死循环
while 1 == 1 :
print ('ok')
9、练习题
(1)使用while循环输入1 2 3 4 5 6 8 9 10
代码如下:
n = 1
while n < 11:
if n == 7 :
pass
else:
print (n)
n = n + 1
print ("-----end-----")
(2)求1-100的所有数的和
n = 1
s = 0
while n < 101 :
s = s + n
n = n + 1
print ( s )
(3)输出1-100内的所有奇数
n = 1
while n < 100 :
temp = n % 2
if temp == 0 :
pass
else :
print (n)
n = n + 1
(4)输出1-100内的所有偶数
n = 1
while n < 101 :
temp = n % 2
if temp == 0 :
print (n)
else :
pass
n = n + 1
(5)求1-2+3-4+5...99的所有数的和
n = 1
s = 0
while n < 100 :
temp = n % 2
if temp == 0 :
s = s - n
else :
s = s + n
n = n + 1
print (s)
(6)用户登录(三次机会重试)
自己写的:
user_name = "Mary"
user_password = "123"
name = input ("请输入你的用户登录名:")
password = input ("请输入你的登录密码")
count = 1
while count < 3 :
if name != user_name :
input ("请输入你的用户登录名:")
else:
print("用户登录名正确")
if password != user_password :
input ("请输入你的登录密码")
else:
print ('用户密码正确')
count = count + 1
老师讲的:
count = 0
while count < 3 :
user_name = input (">>>")
user_pwd = input (">>>")
if user_name == "Mary" and pwd == "123" :
print ("欢迎登录")
break
else :
print ("用户名或密码输入错误")
count = count + 1
print ("请修改用户名或密码")