【Python自学笔记】EX1-EX10 python的输出等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_24994943/article/details/81915627

# ex1 直接输出字符串,用print函数
# ex2 python的注释用“#”
print("Hello World")  # 可以注意到python不加分号结尾

# ex3 python的数字和数学计算
# python的数字变量也无需考虑类型,为保证精确度,在计算除法时,python似乎会把整型相除的结果输出为浮点型
# 而其他的运算则根据变量的类型来决定
a = 10
b = 5
c = 2.5
print("a/b =",a/b) #2.0
print("a+b =",a+b) #15
print("a-b =",a-b) #5
print("a*b =",a*b) #50
print("a+c =",a+c) #12.5
# python输出逻辑表达式,直接输出判断的结果(True or False)
print("a > b?",a>b)
print("a < b?",a<b)

# ex4 python的变量和变量名
# python的变量无需再变量名前加变量类型,如
car = 100
space_in_a_car = 4.0
my_name = 'LudoArtificis'

# ex5 python输出变量的方式有多种
print(f"My name is {my_name}.") # My name is LudoArtificis.
print("My name is {}.".format('LudoArtificis')) # My name is LudoArtificis.
print("My name is",my_name,".") # My name is LudoArtificis .

# ex6 python的字符串
# python字符串的合并操作
types_of_people = 10
x = f"There are {types_of_people} types of people."
print(x)
hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))

w = "This is the left side of..."
e = "a string with a right side."
print(w + e)

# ex7 python更多输出方式
print("." * 10) # 输出十个.。即..........

w1 = "H"
w2 = "e"
w3 = "l"
w4 = "l"
w5 = "o"
w6 = "W"
w7 = "o"
w8 = "r"
w9 = "l"
w10 = "d"
print(w1 + w2 + w3 + w4 + w5, end = ' ')
print(w6 + w7 + w8 + w9 + w10)
# 用end = ' '使得原本输出的换行符改成了空格
# 输出Hello World

# ex8 格式化输出
formatter = "{} {} {} {}"

print(formatter.format(1, 2, 3, 4)) # 1 2 3 4
print(formatter.format("One", "Two", "Three", "Four")) # One Two Three Four
print(formatter.format(True, False, False, True)) # True False False True
print(formatter.format(formatter, formatter, formatter, formatter)) # {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
print(formatter.format(
    "Try your",
    "Own text here",
    "Maybe a poem",
    "Or a song about fear"
)) # Try your Own text here Maybe a poem Or a song about fear

# ex9 多行输出(使用换行符或两个""")
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days: ", days)
print("Here are the months: ", months)

print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5,or 6.
""")
# 打印出来也是多行文字,如下
# There's something going on here.
# With the three double-quotes.
# We'll be able to type as much as we like.
# Even 4 lines if we want, or 5,or 6.

# ex10 转义字符
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
"""

quote_cat = "I'll print a \' and a \" ."

print(tabby_cat) #     I'm tabbed in.
print(persian_cat)
# I'm split
# on a line.
print(backslash_cat) # I'm \ a \ cat.
print(fat_cat)
# I'll do a list:
#   * Cat food
#   * Fishies
#   * Catnip
#   * Grass
print(quote_cat) # I'll print a ' and a " .

猜你喜欢

转载自blog.csdn.net/sinat_24994943/article/details/81915627