python基础
1.第一句python
"hello world"
python后缀名可以任意
以后文件后缀名是 .py
2.两种执行方式
python解释器 py文件路径
python 进入解释器:实时输入并执行(临时)
3.解释器路径
#!/usr/bin/env python
4.编码
# -*- coding:utf8 -*-
ascill 00000000
& 00000001
unicode 0000000000000000+
& 0000000000000001
中 001000000000000111110010
utf-8 能用多少表示就是用多少表示
& 00000001
中 001000000000000111110010
Python3 无需关注
Python2 每个文件中只要出现中文,头部必须加
5.执行一个操作
提醒用户输入:用户和密码
获取用户名和密码,检测:用户名=root 密码=root
正确:登录成功
错误:登陆失败
u = input(“用户名:”)
p = input(“密码:”)
if u == 'root' and p == 'root'
print("登录成功")
else:
print("登录失败")
6.变量名
- 数字
- 字母
- 下划线
数字不能开头
不能是关键字
不要与python内部的东东重叠
7.条件语句
缩进用4个空格
n1 = input('>>>')
a.
if "alex" == "alex":
n2 = input('>>>')
if n2 == "确认":
print('alex SB')
else:
print('alex DB')
else:
print('error')
b.
if 条件1:
pass
elif 条件2:
pass
elif 条件3:
pass
else:
pass
print('end')
c. 条件1
and or
if n1 == "alex" or n2 == "alex!23":
print('OK')
else:
print('OK')
pass 代指空代码,无意义,仅仅用于表示代码块
8.基本数据类型
字符串 - n1 = "alex" n2 = 'root' n3 = """eric"""
数字 - age=21 weight = 64 fight = 5
加减乘除等:
字符串:
加法:
n1 = "alex"
n2 = "sb"
n4 = "db"
n3 = n1 + n2 + n4
# "alexsbdb"
乘法:
n1 = "alex"
n3 = n1 * 10
数字:
n1 = 9
n2 = 2
n3 = n1 + n2
n3 = n1 - n2
n3 = n1 * n2
n3 = n1 / n2
n3 = n1 % n2
n3 = n1 ** n2
出题:
11 12 13 ...
num = 12
n = num % 2
if n == 0:
print('偶数')
else:
rint('奇数')
9.循环
while
while true:
print(“ok”)
10作业
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 < 101:
temp = n % 2
if temp == 0:
pass
else:
print(n)
n = n + 1
print('----end----')
4、输出 1-100 内的所有偶数
n = 1
while n < 101:
temp = n % 2
if temp == 0:
print(n)
else:
pass
n = n + 1
print('----end----')
5、求1-2+3-4+5 ... 99的所有数的和
n = 1
s = 0 # s是之前所有数的总和
while n < 100:
temp = n % 2
if temp == 0:
s = s - n
else:
s = s + n
n = n + 1
print(s)
6、用户登陆(三次机会重试)
n = 0
while n < 3 :
u = input("用户输入:")
p =input("密码:")
if u == 'root' and p =='123456':
print("欢迎登录")
print("........")
break
else:
n=n+1
print("输入的用户名或密码错误")
continue
浙公网安备 33010602011771号