python基础知识总结
变量的命名规范
- 不能以数字开头
- 不能是python中的关键字
python注释
- 单行注释
# [代码块]
- 多行注释
'''
for tempName in fileNames:
print(tempName[tempName.rfind(".")+1:])
'''
如果程序中含有中文则在开头添加注释 #encoding = utf-8
python变量类型
Number
- int (有符号整型)
- long(长整型【也可以表示八/十六位进制】)
- float(浮点型)
- complex(复数)
Boolean
- true
- false
其他
- String
- List
- Tuple(元组)
- Dictionnary(字典)
python格式化输出
score = 100
print("my English Score is %d"%score)
# my English Score is 100
也可以作运算符运算,end=’’ 表示取消自动换行
print("%d*%d=%d "%(j,i,j*i),end='')
print("="*30)
也可以写为
for i in range(times):
print("开始执行第", str(i + 1),"次下拉操作")
python输入
keywords = input("请输入密码")
等同于Java的
Scanner scan = new Scanner(System.in);
String read = scan.nextLine();
注:py2中的input返回的是数字,py2中的raw_input 等于py3中的 input
python添加延时功能
import time
time.sleep(3) #单位S
python中的运算符
特殊的部分
python中
- 10/4 = 2.5
- 10/3 = 3.333333…
在Java中只有float/long等参与计算才会取小数部分,若只有Int只取商
使用 // 取商
- 9//2 = 4
- 9.0//2.00 = 4.0
使用 ** 取幂数
a
∗
∗
b
=
a
b
a**b = a^{b}
a∗∗b=ab
- 2**3 = 8
python 中没有自增自减运算符 a-- a++ --a ++a
逻辑运算符
and or not
python变量赋值
一次性赋值多个变量
a,b,c = 99,98,97
使用python产生随机数
python
import random
num = random.randInt(4,6) #随机产生4-6中间的数
java
//生成一个长度为n的随机数数组
public static int[] getArr(int n){
int[] arr = new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=(int)(Math.random()*100);
}
return arr;
}
pycharm常用快捷键
- Ctrl+Space 代码补全提示
- Ctrl+Alt+Space 快速导入任意类
- Ctrl+Shift+Enter 语句完成
- Ctrl+/ 注释/反注释