(一)Python学习之初识
(1)输入与输出
1.input() :输入,程序永远等待,直到用户输入了值;
input('请输入账号')
2.print():输出;
print("hello word!")
(2)变量名
1.变量名由字母、数字、下划线组成;
2.注意:
(1) 变量名不能用数字开头;
(2) python的关键字不能使用;
(3) 最好不要和python内置的东西重复;
(3)条件语句
if 条件:
内部代码块
else:
内部代码块
1.代码块::后下一行具有相同缩进(一般缩进为四个空格);
2.可以嵌套;
3.elif:和if并列;
inp = input('请输入编号')
if inp == '0':
print('1111')
elif inp == '1':
print('2222')
elif inp == '2':
print('3333')
else:
print('4444')
4.pass:该内部代码块不执行,直接跳过;
(4)基本数据类型
1.字符串:一对双引号,一对单引号,三对双引号,三对单引号中的内容;
'aaa'
"aaa"
'''aaa'''
"""aaa"""
字符串加法:
n1="ss"
n2="bb"
n3=n1+n2
print(n3)
字符串乘法:
n1="al"
n2=n1*3
print(n2)
2. 数字
age=13
运算:
加:+,减:-,乘:*,除:/,取余:%,乘方:**,取商://;
(5)循环语句
1.while循环
while 条件:
内部代码块
2.continue:终止当前循环,开始下一次循环;
3.break:终止所有循环,直接跳出循环;
(6)练习
练习1:使用while循环输入 1 2 3 4 5 6 8 9 10
count = 1
while count < 11:
if count != 7:
print(count)
else:
print(" ")
count = count + 1
练习2:求1-100的所有数的和
count = 0
temp = 1
while temp <101:
count = count + temp
temp = temp +1
print(count)
练习3:输出1-100内的所有基数
count = 1
while count <101:
if count % 2 == 0:
pass
else:
print(count)
count = count +1
练习4:输出1=100内的所有偶数
count = 1
while count <101:
if count % 2 == 0:
print(count)
else:
pass
count = count +1
练习5:求1-2+3-4+5…99的所有数的和
count = 0
temp = 0
while temp <100:
if temp % 2 == 0:
count = count - temp
else:
count = count +temp
temp = temp + 1
print(count)
练习6:用户登录(三次机会重试)
count = 0
while count < 3:
user = input('请输入账号')
pwd = input('请输入密码')
if user == 'alex' and pwd == '123':
print('............')
break
else:
print('用户名或密码错误')
count = count +1