1、注释
当行注释:# 被注释内容
多行注释:’’‘被注释内容’’’,或者""“被注释内容”""
2、名字定义方式
#驼峰体
AgeOfOldboy = 56
NumberOfStudents = 80
#下划线
age_of_oldboy = 56
number_of_students = 80
3、程序交互
name = input(“What is your name?”)
age = input(“How old are you?”)
hometown = input(“Where is your hometown?”)
print("Hello ",name , "your are ", age , “years old, you came from”,hometown)
4、基础类型
int 32位为32位
64位为64位
str 在Python中,加了引号的字符都被认为是字符串!
可以使用type(变量) 查看数据类型
字符串拼接
字符串可以进行相加和相乘操作,字符串的拼接只能是双方都是字符串,不能跟数字或其它类型拼接。
>>> name
'Alex Li'
>>> age
'22'
>>> name + age #相加其实就是简单拼接
'Alex Li22'
>>> name * 10 #相乘其实就是复制自己多少次,再拼接在一起
'Alex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex Li'
格式化输出
要求:
------------ info of Alex Li -----------
Name : Alex Li
Age : 22
job : Teacher
Hobbie: girl
------------- end -----------------
代码实现:
name = input("Name:")
age = input("Age:")
job = input("Job:")
hobbie = input("Hobbie:")
info = '''
------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name
Name : %s #代表 name
Age : %s #代表 age
job : %s #代表 job
Hobbie: %s #代表 hobbie
------------- end -----------------
''' %(name,name,age,job,hobbie) # 这行的 % 号就是 把前面的字符串 与拓号 后面的 变量 关联起来
print(info)
以上代码实现会报错,因为input接收的所有输入默认都是字符串格式,需要将age从字符串转为int。
注意:input接收的所有输入默认都是字符串格式!
str转成int
age = int( input(“Age:”) )
print(type(age))
占位符
问题代码
msg = "我是%s,年龄%d,目前学习进度为80%"%('金鑫',18)
print(msg)
正常代码
msg = "我是%s,年龄%d,目前学习进度为80%%"%('金鑫',18)
print(msg)
%是占位符需要输出多加一个%才能输出。
基本运算符
计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算,今天我们暂只学习算数运算、比较运算、逻辑运算、赋值运算。
a =1,b=2.
算术运算符:
比较运算:
赋值运算:
逻辑运算:
在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。
1,3>4 or 4<3 and 1==1
2,1 < 2 and 3 < 4 or 1>2
3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
in,not in :
#print('喜欢' in 'dkfljadklf喜欢hfjdkas')
#print('a' in 'bcvd')
#print('y' not in 'ofkjdslaf')