python基础
1.后缀名问题:
后缀名是可以是任意,导入模块时后缀必须是.py => 以后文件后缀名是 .py
2.执行方式:
python解释器 py文件路径:
python D:/test.py
python 进入解释器:
实时输入并获取到执行结果
3.解释器路径:
#!/usr/bin/env python
4.编码问题:
# -*- coding:utf8 -*-
Python3 无需关注
Python2 每个文件中只要出现中文,头部必须加
5.执行一个操作: 提醒用户输入:用户和密码 获取用户名和密码,检测:用户名=root 密码=root 正确:登录成功 错误:登陆失败
a. input的用法,永远等待,直到用户输入了值,就会将输入的值赋值给一个东西
6.变量名: 由字母 数组 下划线组成,数字不能开头;不能是关键字,最好不要和python内置的东西重复 ***
7.条件语句
缩进用4个空格
a:
n1 = input('>>>') if "good" == "good": n2 = input('>>>') if n2 == "确认": print('good ') else: print('very good') else: print('error')
注意: = 赋值 == 比较
b:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 num = input("请输入1-9之间的数字:") 2 if num==1: 3 print(num) 4 pass 5 elif num==2: 6 print(num) 7 pass 8 elif num==3: 9 print(num) 10 pass 11 else: 12 print(num) 13 pass 14 15 print('end')
c: and or pass 代指空代码,无意义,仅仅用于表示代码块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 n1 = input("请输入n1:") 2 n2 = input("请输入n2:") 3 if n1 == "sqy" or n2 == "sqy!23": 4 print('OKOK') 5 else: 6 print('OK')
8.基本数据类型:
字符串 - n1 = "alex" n2 = 'root' n3 = """eric""" n4='''tony'''
数字 - age=21 weight = 64 fight = 5
加减乘除:
字符串:
加法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 n1 = "laowang" 2 n2 = "sb" 3 n4 = "db" 4 n3 = n1 + n2 + n4 5 print(n3)
乘法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 n1 = "yuge" 2 n3 = n1 * 10 3 print(n3)
数字:
n3 = n1 + n2
n3 = n1 - n2
n3 = n1 * n2
n3 = n1 / n2 除尽 9/2=4.5
n3 = n1 % n2 求余 7%2=1
n3 = n1 ** n2 幂乘 2**4=16
9.循环:
死循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 while 1==1: 2 print('ok')