#错误语法if5>2:print("Five is greater than two!")print("Five is greater than two!")#正确方式:if5>2:print("Five is greater than two!")print("Five is greater than two!")
变量命名规则
变量名称必须以字母或者下划线字符开头
变量名称不能以数字开头
变量名只能包含数字,字母,字符,下划线
变量名称区分大小写
变量规则
Python允许一行中为多个变量赋值.例:
x,y,z='zh','has','cats'print(x)print(y)print(z)
python允许两个相同类型的变量相加
x , y ='zh','has'
z = x + y
print(z)#输出结果:zhhas
x =10
y ="Bill"print(x + y)#运行结果:报错
全局变量可以在方法函数内部使用
x ="awesome"defmyfunc():print("Python is "+ x)
myfunc()
方法函数内部创建和全局变量一样的变量名,那只能在此方法函数内部使用。
在方法函数内部创建全局变量使用global
defmyfunc():global x
x ="fantastic"
myfunc()print("Python is "+ x)
x =10# int
y =6.3# float
z =2j# complex complex用j作为虚部编写,complex类型不能转换为其他类型
可以使用type获得数据类型
x =20#display x:print(x)#display the data type of x:print(type(x))#输出结果:20<class'int'>
也可以给这些数据设定数据类型
x =tuple(("apple","banana","cherry"))#display x:print(x)#display the data type of x:print(type(x))#输出结果:#('apple', 'banana', 'cherry')#<class 'tuple'>
数值型数据类型转换
x =10
y =6.5
z =1jprint(type(x))print(type(y))print(type(z))
a =float(x)
b =int(y)
c =complex(x)
d =complex(y)print(a)print(b)print(c)print(d)#输出结果:<class'int'><class'float'><class'complex'>10.06(10+0j)(6.5+0j)
可以使用三个单引号或者三个双引号给一个变量赋多行值
a ="""Python is a widely used general-purpose, high level programming language.
It was initially designed by Guido van Rossum in 1991
and developed by Python Software Foundation.
It was mainly developed for emphasis on code readability,
and its syntax allows programmers to express concepts in fewer lines of code."""print(a)#输出结果:
Python is a widely used general-purpose, high level programming language.
It was initially designed by Guido van Rossum in1991and developed by Python Software Foundation.
It was mainly developed for emphasis on code readability,and its syntax allows programmers to express concepts in fewer lines of code.
#python位运算符&and 如果两个位均为1,则将每个位设为1|or 如果两位中的其中一位为1,则将每个位都设为1^ xor 两个二进制相同位数上的数字不同,则返回的数字的二进制在该位置值为1,否则为0~not 反转所有位
<< zero fill left shift 通过从右侧推入0来向左移动,推掉左侧的位
>> Signed right shift 通过从左侧推入最左边的位的副本向右移动,推掉最右边的位