full_name = “john”
age=20
is_new = True
name = input("What is your name? ")
colour = input("What is your favourite colour? ")
print(name + " like " + colour)
2.将字符串转化为整数类型
birthday = input("what is your birthday? ")
age = 2019 - int(birthday)
print(age)
3.type() 展示字符串或整数的类型
birthday = input("what is your birthday? ")
print(type(birthday))
age = 2019 - int(birthday)
print(type(age))
print(age)
4.代码里面敲回车
course = “”"
hello i am zhouzihan
what is your name ?
i am glad to see you
“”"
print(course)
5.打出字符串中某个位置的字母
course = “python for beinner”
print(course[0]) //打出第一个字母
print(course[-1])
print(course[0:4])
print(course[0:]) //打出全部字符
print(course[1:]) //打出除了第一个以外的全部字母
another = course[:] //another是course的克隆
6.打出带中括号的字符,用f" "
first = “john”
last = “smith”
message = first + “[” + last + “]” + " is a coder"
msg = f"{first} [ {last} ] is a coder"//简化写法
7.计算字符串中字符的数目和列表中的项目数 len( )
first = “john”
last = “smith”
msg = f"{first} [ {last} ] is a coder"
len(msg)
print(msg)
print(len(msg))
8.专用于字符串的某些函数
将字符串全部转换为大写字母 .upper( )
将字符串全部转换为小写字母 .lower( )
每个单词的首字母大写 .title( )
字符串的替换 .replace(" ", " ")
它们并不会改变原来的变量,而是创造一个新的副本并改变它
first = “john”
last = “smith”
msg = f"{first} [ {last} ] is a coder"
print(msg)
print(msg.upper())
i = input("Please type: ").lower() //输入时就可以转换字符串
print(msg.find(“h”))
print(msg.replace(“hn”, “wow”))
print(msg)
9.查找字符串中的某个字符的位置 .find(" ")
注意它返回的结果是位置,所以是从0开始
first = “john”
last = “smith”
msg = f"{first} [ {last} ] is a coder"
print(msg.find(“h”))
10 .检查字符串中是否有某些字符 in
它返回的是一个布尔值,与 .find 不同
first = “john”
last = “smith”
msg = f"{first} [ {last} ] is a coder"
print(“john” in msg)
11.进行四则运算
print(10 + 3) =13
print(10 * 3) =30
print(10 / 3) =3.3333333
print(10 // 3) =3 //除完后取整数
print(10 % 3) =1 //除完后取余数
print(10 ** 3) =1000 //求幂次方
12.增广赋值运算符
x += 3
//x=x+3
x -=3
//x=x-3
13.x=-2.9
四舍五入运算符print(round(x))
取绝对值函数 print(abs(x))
14.导入数学函数
import math
print(math.floor(2.9))
15.if语句
is_hot = False
is_cold = True
if is_hot:
print(“Enjoy your day”)
elif is_cold:
print(“put on more clothes”)
else:
print(“have a good day”)
if a and b: //逻辑与
if a or b : //逻辑或
if a and not b //a与非b为真