1,语法突出
编写程序时,python编译器会以各种方式突出程序的不同部分。如,print是一个函数的名称,因此将其显示为蓝色;这种功能称为:语法突出。
2,变量
message = “hello world”
print(message)
#首先讲字符串赋给message这个变量,并使用print函数将message这个变量里储存的内容打印出来
2,1 变量的命名与使用
2,1,1 变量名只包括:字母、数字、下划线,数字不能作为变量的开头
2,1,2 变量名不能包含空格
2,1,3 不能将python的关键字和函数用作变量名。如,print
2,1,4 变量名应该既简单又形象。如name比n好
2,1,5 慎用小写字母l和o,会被误认为数字1和0
3,字符串—python中用引号括起来的都是字符串
3,1 使用方法修改字符串的大小写
msg.py
msg = "hello world"
print(msg.title())
print(msg.Upper())
print(msg.lower())
>Hello World
>HELLO WORLD
>hello world
3,2 合并字符串
name.py
first_name = "jay"
last_name = "zhou"
print(first_name + second_name)
3,3 使用制表符\t和换行符\n来添加空白
print("hello")
>hello
print("\thello")
> hello
print("languages:\nC\nPython")
>languages:
C
Python
3,4 删除空白(rstrip()-删除右侧空白,lstrip()-删除左侧空白,strip()删除两侧空白)
3,5 使用字符串时避免语法错误
主要是单引号‘’和双引号“”的使用
4,数字(整型,浮点型,使用str()避免类型错误)
birthday.py
age = 23
message = "happy"+age+"rd birthday!"
print(message)
显示错误--TypeError: must be str, not int
这里修改成
age = 23
message = "happy"+str(age)+"rd birthday!"
print(message)
5,注释
日常代码编写过程中,注意注释的使用。
课后题:
# 2-1 打印一条消息
msg = "hello world!"
print(msg)
# 2-2 打印多条消息
msg = "hello everyone!"
print(msg)
msg = "hello pythoner!"
print(msg)
# 2-3 个性化消息-消息中插入变量
name = "LiLy"
print("hello," + name + "!")
# 2-4 调整名字的大小写
name = "LiLy"
print("hello," + name.lower() + "!")
print("hello," + name.upper() + "!")
print("hello," + name.title() + "!")
# 2-5 ,#2-6 输出一句名人名言
name = "albert einstein"
word = "A person who never made a mistake never tried anything new."
print(name.title() + " once said," + '"' + word + '"')
# 2-7 剔除名字中的空白,并打印出来
name = "\talbert\t\n"
print(name.lstrip())
print(name.rstrip())
print(name.strip())
# 2-8 数字运算
print(5+3)
print(11-3)
print(4*2)
print(16/2)
# 2-9 输出最喜欢的数字
faverate = 8
print("my faverate number is : " + str(faverate))
# 2-11 Python 之禅
import this