基础
1:第一个语句
print("HelloWorld")
2:两种执行方式
python解释器py文件路径
python 进入解释器
实时输入并获取到执行结果
3:解释器路径
linux中
#!/usr/bin/env python
4:编码 #!-*-coding:utf8-*-
python3无需加
python2每个文件中只要出现中文,头部必须加。
5:执行一个操作
提醒用户名输入:用户名和密码。
获取用户名和密码,检测:用户名=yhm,密码=123
正确:登录成功
错误:登录失败
代码:
a = input("请输入用户名:")
b = input("请输入密码:")
if a=="yhm":
if b=="123":
print("登录成功")
else:
print("登录失败")
else:
print("登录失败")
input的用法,永远等待,直到用户输入了值,就会将输入的值赋值给一个变量
6:变量名
——字母
——数字
——下划线
ps:数字不开头
不能是关键字
最好不要和python内置东西重复。
7:字符串(引号):
name1 = "******"
name2 = '''****'''
name3 = """***"""
加法:
n1 = "wo"
n2 = "ta"
n3 = n1+n2
结果:wota
乘法:
n1 = "wo"
n2 = n1*4
结果:wowowowo
8:数字:
a = 20
加、减、乘、除、次方(**)、求余(%)、商(//) 9:循环:(在python中注意缩进) (一)打印:1 2 4 5 6 8 9 10 i = 1 while i<=10 : if i ==1 : print(i,end = " ") else: print( ) i +=1 #(在循环中必须有破坏循环条件的语句) (二)打印:1到100 i = 1 while i <=100 : print(i) i +=1 (三)求1-2+3-4+5到99 i = 1 j = 1c = 0 while i<=99 : if i%2==0: j = -1 else: j = 1 c = c + i*j i +=1 print(c) #如有错,欢迎评论交流