官网
国内相关贴子
python --version
python import sys sys.path
Ctrl +/
File->Setting->Editor->Code Style->File and Code Templates->Python Script 后面加上 # *coding:utf-8 * 即可
• +
plus,加号
• -
minus,减号
• /
slash,斜杠
• *
asterisk,星号
• %
percent,百分号
• <
less-than,小于号
• >
greater-than,大于号
• <=
less-than-equal,小于等于号
• >=
greater-than-equal,大于等于号
# -*- coding:utf-8 -*- '''练习6''' types_of_people = 10 # 定义一个变量并赋值 x = f"There are {types_of_people} types of people " # 定义变量x,并在赋值时引用另一个变量且使用.format()函数使其能被其他函数引用 binary = "binary" # 为 binary 赋值 do_not="don't" # 为do_not赋值 y = f"Those who know {binary} and those who {do_not}." # 为变量"y" 赋值时引用"binary","do_not"且用.format()函数使能被其他函数引用 print(x) #打印变量'x'的值 print(y) #打印变量'y"的值 print(f"I said:{x}") print(f"I also said: '{y}'") hilarious = False Joke_evaluation = "Isn't that joke so funny?!{}" w = "This is the left side of..." e = "a string with a right side." print(w + e)
知识点总结:
1.当为一个变量赋值,且希望之后可以引用它,可以用.format()函数;
2.当一个新的变量引用之前的变量时,使用{};
'''练习7''' print("Mary had a little lamb.") print("Its fleece was white as {}.".format('snow')) print("And everywhere that Mary went.") print("."* 10) # what"d that do ? 答:打印10个"." end1 = "C" end2 = "h" end3 = "e" end4 = "e" end5 = "s" end6 = "e" end7 = "B" end8 = "u" end9 = "r" end10 = "g" end11 = "e" end12 = "r" #watch that comma at the end. try removeing it to see what h print(end1 + end2 + end3 + end4 + end5 +end6,end= ' ' ) print(end7 + end8 + end9 + end10 + end11 +end12)
知识点总结:
x = f("test1") y = test 2 {x}test3
y=("test2{}test2”.format('test1'))
'''练习8''' formatter = "{}{}{}{}" print(formatter.format(1,2,3,4)) print(formatter.format("one,","two,","three,","four")) print(formatter.format(True,False,False,True)) print(formatter.format(formatter,formatter,formatter,formatter)) print(formatter.format( "Try your\n", "Own tex here\n", "Maybe a poem\n", "Or a song about fear" ))
知识总结:见练习6
'''练习10''' tabby_cat = "\tI'm tabbed in." persian_cat = "I'm split\non a line." backslash_cat = "I'm \\ a \\ cat." fat_cat = """ I'll do a list: \t* Cat food \t* Fishies \t* Catnip\n\t* Grass """ print(tabby_cat) print(persian_cat) print(backslash_cat) print(fat_cat)
知识点总结:
"""
开始,"""
结尾,进行段落打印,print(""" 也是一种转义字符""")
'''练习11''' print("How old are you?") age = input() print("How tall are you ?") height = input() print("How much do you weigh?") weight = input() print(f"So, you're {age} old, {height} tall and {weight} heavy.")
知识点总结:
当print("一串字符串",end=' ')
时,打印结果与input内容在同一行换行,
当print("一串字符串")
时,打印结果换行后再进行input
'''练习11''' age = input ("How old are you?") height = input("How tall ara you?") weight = input("How much do you weigh?") F = f"So,you're {age} old,{height} tall and {weight} heavy." print(F)
知识点总结:
可以直接在函数input()
中输入提醒,同时将输入的值赋予一个函数;
如果想用输入的值做数学运算,可以使用"int"
整形函数或其他类型的函数将输入字符变为数字,例:
x = int(input("请输入一个整形数值")) y = float(input("请输入一个小数")) print("他们的乘积为{:.3f}".format(x*y)) P= x*y print("他们的乘积为%.2f" %P)
注:注意以上代码中指定浮点函数输出小数点后位数的方式有两种