print("Hello Python world") #print() 函数
message = "Hello Python world" #message 变量
print(message)
message = "Hello Python world again" #message 变量值可以修改
print(message)
#变量命名规则
#字母、数字、下划线组成;只能以字母或下划线开头
#不能包含空格
#不能用关键字和函数名
#简短,且具有描述性 如 student_name
#慎用小写字母l和大写字母O,区分1和0
print("--------------------------------------------")
#字符串 单引号或双引号
name = "i love china"
#单词首字母大写: 方法title()
print(name.title())
#全部大写 upper()
print(name.upper())
#全部小写 lower()
print(name.lower())
print("--------------------------------------------")
#字符串合并 输出
first_name = "zhang"
last_name = "san"
full_name = f"{first_name} {last_name}"
print(full_name)
#f是format的简写,python3.6及以上版本支持
#3.5版本及以下使用方法如下
# full_name = "{} {}".format(first_name, last_name)
print(f"Hello,{full_name.title()}!")
#f字符串 创建变量
message = f"Hello,{full_name.title()}!"
print(message)
print("--------------------------------------------")
# \n 换行 \t 制表符
print("\tPyhon")
print("hello\nhow are you today\nfine,thank you!")
print("--------------------------------------------")
# 删除空格
# 末尾空格 rstrip()
# 开头空格 lstrip()
message = " hello "
message = message.rstrip()
print(message)
message = message.lstrip()
print(message)
print("--------------------------------------------")
# 整数 + - * /
print(2 + 3)
print(3 - 2)
print(2 * 3)
print(3 / 2)
# ** 乘方
print(3 ** 2) #3的2次方
print(3 ** 3) #3的3次方
print("--------------------------------------------")
# 浮点数 结果包含的小数位数可能是不确定的
print(0.1 + 0.1)
print(0.2 + 0.1)
print(2 * 0.1)
print(3 * 0.1)
print("--------------------------------------------")
# 任意数相除,结果总为浮点数;
# 有浮点数参与的运算,结果也为浮点数
# 数值很大,可用下划线给数字分组
value = 1_000_000_000_000
print(value)
print("--------------------------------------------")
# 同时给多个变量赋值,用逗号隔开
x, y, z = 0,1,2
print(x,y,z)
print("--------------------------------------------")
# 常量,一般定义时使用全大写字母
MAX_VALUE = 1000
print("--------------------------------------------")
练习:
message = "I love China"
print(message)
message = "I love China"
print(message)
message = "I love China very much"
print(message)
name = "Eric"
print(f"Hello {name},would you like to learn some Python today?")
name = "zhang san"
print(name.title())
print(name.upper())
print(name.lower())
print('Albert Einstein once said, "A person who never made a mistake')
print('never tried anything new."')
name = "\tEric Matthes\n"
print("Unmodified:")
print(name)
print("\nUsing lstrip():")
print(name.lstrip())
print("\nUsing rstrip():")
print(name.rstrip())
print("\nUsing strip():")
print(name.strip())
fav_num = 8
msg = f"My favorite number is {fav_num}."
print(msg)
import this
# The Zen of Python, by Tim Peters
# Beautiful is better than ugly.
# Explicit is better than implicit.
# Simple is better than complex.
# Complex is better than complicated.
# Flat is better than nested.
# Sparse is better than dense.
# Readability counts.
# Special cases aren't special enough to break the rules.
# Although practicality beats purity.
# Errors should never pass silently.
# Unless explicitly silenced.
# In the face of ambiguity, refuse the temptation to guess.
# There should be one-- and preferably only one --obvious way to do it.
# Although that way may not be obvious at first unless you're Dutch.
# Now is better than never.
# Although never is often better than *right* now.
# If the implementation is hard to explain, it's a bad idea.
# If the implementation is easy to explain, it may be a good idea.
# Namespaces are one honking great idea -- let's do more of those!