Python第二章

# 打印
print("Hello World!")

# 2-1 变量,打印函数
message = 'Hello Python World!'
print(message)

# 2-2 修改变量,再打印出来
a = 'Hello Python World!'
print(a)

# 重新定义变量,变量刷新值
# 变量注意事项:
# 1 一个变量只能引用一个数值
# 2 命名规范
# 字母数字下划线组成,只能字母或者下划线开头
# 见名知意
# 驼峰标识
# 非关键字
"""'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while',
'with', 'yield'"""
# 区分大小写
# 3 变量名使用之前一定要赋值

# 方式1:变量名 = 值
a = 1
print(a)
# 方式2:变量名1, 变量名2 = 值1, 值2
a, b = 2, 3
print(a, b)
# 方式3:变量名1 = 变量名2 = 值
a = b = 3
print(a, b)

# 6 + 1; 6 + 2; 6 + 3; 6 + 4; 6 + 5; 用变量num方便代码维护修改num的值
# 快速复制一行:把鼠标放在该行末尾,按Ctrl + D即可
# 参数替换:按Ctrl + R进行替换操作
print(6 + 1)
print(6 + 2)
print(6 + 3)
print(6 + 4)
print(6 + 5)

num = 8
print(num + 1)
print(num + 2)
print(num + 3)
print(num + 4)
print(num + 5)

# 节省空间(磁盘、空间)
# 打印十遍“社会我政哥,人狠话不多 1”
# print("社会我政哥,人狠话不多" + "1")
# print("社会我政哥,人狠话不多" + "2")
# print("社会我政哥,人狠话不多" + "3")
# print("社会我政哥,人狠话不多" + "4")
# print("社会我政哥,人狠话不多" + "5")
# print("社会我政哥,人狠话不多" + "6")
# print("社会我政哥,人狠话不多" + "7")
# print("社会我政哥,人狠话不多" + "8")
# print("社会我政哥,人狠话不多" + "9")
# print("社会我政哥,人狠话不多" + "10")

name = "社会我政哥,人狠话不多"
print(name + "1")
print(name + "2")
print(name + "3")
print(name + "4")
print(name + "5")
print(name + "6")
print(name + "7")
print(name + "8")
print(name + "9")
print(name + "10")

# 读取关键字列表并打印输出
import keyword

print(keyword.kwlist)

# 2-2 多条简单信息打印并修改变量值
message = 'Hello Python World!'
print(message)

message = "Hello Python Crash Course reader!"
print(message)

# 2.3 字符串(引号括起来的字符,单引号双引号均可)
print("This is a string.")
print('This is also a string.')

# 2.3.1 使用方法修改字符串的大小写
name = "ada lovelace"
print(name.title())
print(name.upper())
print(name.lower())

# 2.3.2 合并(拼接)字符串
first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
print("Hello, " + full_name.title() + "!")
message = "Hello, " + full_name.title() + "!"
print(message)

# 2.3.3 使用制表符或者换行符来添加空白
print("python")
print("\tpython")
print("Languages:\nPython\nC\nJavascript")
print("Languages:\n\tPython\n\tC\n\tJavascript")

# 2.3.4 删除空白
favorite_language = " python "
# 删除右端空白
print(favorite_language.rstrip())
# 删除左端空白
print(favorite_language.lstrip())
# 删除两端空白
print(favorite_language.strip())

# 暂时删除字符串中的空白
favorite_language = 'python '
print(favorite_language)
print(favorite_language.rstrip())
favorite_language = ' python'
print(favorite_language)
print(favorite_language.lstrip())
# 永久删除字符串中的空白,进行赋值操作
favorite_language = 'python '
print(favorite_language)
favorite_language = favorite_language.rstrip()
print(favorite_language)
favorite_language = ' python'
print(favorite_language)
favorite_language = favorite_language.lstrip()
print(favorite_language)

# 2.3.5 使用字符串是避免语法错误,单引号无法确定字符串结束位置,双引号可以
# message = 'One of Python's strength is its diverse community.'
# print(message)
message = "One of Python's strength is its diverse community."
print(message)

# 2-3 个性化消息
name = "Eric"
print("Hello " + name + ", would you like to learn some Python today?")

# 2-4 人名,小写、大写、首字母大写的方式显示
name = "Top Tan"
print(name.lower())
print(name.upper())
print(name.title())

# 2-5 打印名人名言
print('Albert Einstein once said, "A person who never made a mistake never tried anything new. "')

# 2-6 打印名人名言
name = "Albert Einstein"
saying = '"A person who never made a mistake never tried anything new. "'
print(name + " once said, " + saying)
famous_people = "Shakespeare"
message = ' thus said, "Love all, trust a few, do wrong to none."'
print(famous_people + message)

# 2-7 使用\t、\n至少各一次打印人名
name = " \n\t\tAlbert Einstein\t\t\n "
print(name)
# 去掉后面的"\t\t\n "并打印
a = name.rstrip()
print(a)
# 去掉前面的" \n\t\t"并打印
b = name.lstrip()
print(b)
# 去掉前面的" \n\t\t"和去掉后面的"\t\t\n "并打印
c = name.strip()
print(c)

# 2.4.1 整数
a = 2 + 3
b = 3 - 2
c = 2 * 3
d = 3 / 2
e = 3 ** 2 # 三的二次方
f = 2 + 3 * 4
g = (2 + 3) * 4
print(a, b, c, d, e, f, g)

# 2.4.2 浮点数(注意:结果包含的小数位可能是不确定的)
a = 0.1 + 0.1
b = 2 * 0.1
c = 0.2 + 0.1
d = 3 * 0.1
print(a, b, c, d)

# 2.4.3 使用函数str()避免类型错误
age = 23
message = 'Happy ' + str(age) + 'rd birthday!' # 不能直接是message = 'Happy ' + age + 'rd birthday!',这样类型不一致会报错
print(message)

# 2-8 使用加减乘除运算使得结果均为数字8
a = 3 + 5
b = 9 - 1
c = 4 * 2
d = 16 / 2 # 运行后小数点为啥会保留一位
print("\n", a, "\n", b, "\n", c, "\n", d)

# 2-9 输出最喜欢的数字
favorite_num = 666
print(str(favorite_num) + ' is my favorite number!') # 不能直接是print(favorite_num + ' is my favorite number!'),这样类型不一致会报错
print(favorite_num.__str__() + ' is my favorite number!') # 或者这样编写

# 2.5.1 如何编写注释(单行注释可按Ctrl + /或者在代码行末端加两个空格和一个#进行注释,代码行注释格式如下:“ # ”,多行注释格式如下:'''(注释部分)'''(三个单引号)或者“““(注释部分)”””(三个双引号)或者多行行首加#号)
# 向大家问好!
print('Hello Python People!')

# 2-10 给自己的程序添加注释
# 作者:燃烧信仰TopTan 日期:2020.04.10
print("有志者,事竟成")
print("滴水穿石,持之以恒")

# 2.6 Python之禅
import this

posted @ 2020-06-08 11:22  燃烧信仰TopTan  阅读(189)  评论(0)    收藏  举报