python编程初学者的良言警句_python学习——python编程:从入门到实践(1,2章)...

1.

print("Hello Python world!")

Hello Python world!

2.

message = "Hello Python world! "

print(message)

message = "Hello Python Crash Course World!"

print(message)

Hello Python world!

Hello Python Crash Course World!

新输入的message并没有取代原来的

3.

变量的命名和使用

只包含字母,数字和下划线,但不能以数字打头

不能包含空格,但可以使用下划线来分隔其中的单词

不能以python关键词和函数名来命名 关键词(False,class,finally,is,return,

None,continue,for,lambda,try,

True,def,from,nonlocal,while,

and,del,global,not,with,

as,elif,if,or,yield,

assert,else,import,pass,

break,except,in,raise)

变量名应既简短又具有描述性

慎用小写字母l和大写字母O,因为他们可能被人错看成数字1和0

4.

字符串:可以单引号,双引号(在包含引号和撇号的句子中灵活使用)

title() 将每个单词的首字母都改为大写(why?因为在文章的title中每个单词的首字母都要大写)

name = "ada lovelace"

print(name.title())

Ada Lovelace

upper()全部大写(upper是比之前更大,so是全部大写)

lower()全部小写(lower是比之前更小,so是全部小写)

print(name.upper())

ADA LOVELACEprint(name.lower())

ada lovelace

合并(拼接)字符串

合并 A+' '+B

first_name = 'ada'

last_name = 'lovelace'

full_name = first_name + ' '+ last_name

print(full_name)

ada lovelace

拼接 "A"+B+"C"

first_name = 'ada'

last_name = 'lovelace'

full_name = first_name + ' '+ last_name

print("Hello," + full_name.title() + "!")

Hello,Ada Lovelace!

简化拼接

first_name = 'ada'

last_name = 'lovelace'

full_name = first_name + ' '+ last_name

message = "Hello," + full_name.title() + "!"

print(message)

Hello,Ada Lovelace!

使用制表符或换行符来添加空白

制表符

print("python")

print("\tpython")

python

python

注意:\t 反斜杠

print("/tpython")

/tpython

换行符

print("python")

print("\npython")

python

python

同时使用

print("python")

print("\n\tpython")

python

python

注意 print("\t\npython") 【 print("\n\tpython")】\t\n顺序不同

python

python

删除空白

确保字符串末尾没有空白,可使用rstrip(),but它是创建副本后删除副本的空白,不改变母本的空白

message = "python "

print(message)

print(message.__sizeof__())

print(message.rstrip())

print(message.rstrip().__sizeof__())

print(message)

print(message.__sizeof__())

python

58

python

55

python

58

要删除母本空白,需将结果返回到母本中

message = "python "

print(message)

print(message.__sizeof__())

message = message.rstrip()

print(message)

print(message.rstrip().__sizeof__())

print(message)

print(message.__sizeof__())

python

58

python

55

python

55

确保字符串开头没有空白,可使用lstrip(),but它是创建副本后删除副本的空白,不改变母本的空白,要删除母本空白,需将结果返回到母本中

name = ' Albert Einstein '

print(name)

print(name.__sizeof__())

print(name.lstrip())

print(name.lstrip().__sizeof__())

print(name)

print(name.__sizeof__())

Albert Einstein

68

Albert Einstein

66

Albert Einstein

68

确保字符串两端没有空白,可使用strip(),but它是创建副本后删除副本的空白,不改变母本的空白,要删除母本空白,需将结果返回到母本中

name = ' Albert Einstein '

print(name)

print(name.__sizeof__())

print(name.strip())

print(name.strip().__sizeof__())

print(name)

print(name.__sizeof__())

Albert Einstein

68

Albert Einstein

64

Albert Einstein

68

使用字符串避免错误

正确的

message = "I'm a beginner in Python"

print(message)

I'm a beginner in Python

错误的 (计算机不是人,它无法正确定位字符串的结束位置,这时需要我们多加注意)

message = 'I'm a beginner in Python'

print(message)

File "C:/Users/25337/PycharmProjects/untitled1/app.py", line 1

message = 'I'm a beginner in Python'

^

SyntaxError: invalid syntax

练习题

2-3 个性化消息:  将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello Eric, would you like to learn some Python today?”。

name = 'Eric'

message = "Hello " + name.title()+ ", would you like to learn some Python today?"

print(message)

Hello Eric, would you like to learn some Python today?

2-4 调整名字的大小写:  将一个人名存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人名。

name = 'jiaxin Lee'

print(name.lower())

print(name.upper())

print(name.title())

jiaxin lee

JIAXIN LEE

Jiaxin Lee

2-5 名言:  找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):

Albert Einstein once said, “A person who never made a mistake never tried anything new.”

message = 'Albert Einstein once said, “A person who never made a mistake never tried anything new.”'

print(message)

Albert Einstein once said, “A person who never made a mistake never tried anything new.”

2-6 名言 2:  重复练习2-5,但将名人的姓名存储在变量famous_person 中,再创建要显示的消息,并将其存储在变量message 中,然后打印这条消息。

name = 'Albert Einstein'

message = name+ ' once said, “A person who never made a mistake never tried anything new.”' 注意要在once前面空一格或在人名后空一格

print(message)

Albert Einstein once said, “A person who never made a mistake never tried anything new.”

2-7 剔除人名中的空白:  存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t" 和"\n" 各一次。

打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。

name = ' \tAlbert Einstein '

print(name.__sizeof__())

print(name.lstrip())

print(name.lstrip().__sizeof__())

print(name.rstrip())

print(name.rstrip().__sizeof__())

print(name.strip())

print(name.strip().__sizeof__())

name = ' \nAlbert Einstein '

print(name.__sizeof__())

print(name.lstrip())

print(name.lstrip().__sizeof__())

print(name.rstrip())

print(name.rstrip().__sizeof__())

print(name.strip())

print(name.strip().__sizeof__())

69

Albert Einstein

66

Albert Einstein

67

Albert Einstein

64

69

Albert Einstein

66

Albert Einstein

67

Albert Einstein

64

5.

数字

整数(+—*/)

print(2+3)

print(2-3)

print(2*3)

print(2/3)

5

-1

6

0.6666666666666666

乘方运算(**)

print(2**3)

6

还支持运算次序

print(2+3*4)

print((2+3)*4)

14

20

浮点数(运算后包含的小数位数可能是不确定的)

print(0.1+0.2)

0.30000000000000004

使用str()函数避免类型错误

例子如下

age = 23

message = "Happy"+ age + "rd Birthday"

print(message)

Traceback (most recent call last):

File "C:/Users/25337/PycharmProjects/untitled1/app.py", line 2, in

message = "Happy"+ age + "rd Birthday"

TypeError: can only concatenate str (not "int") to str

23既可能表示数值23,有可能是数字2和3,so 我们要明确指出23的类型

age = 23

message = "Happy "+ str(age) + "rd Birthday"

print(message)

Happy 23rd Birthday

7.

注释

#向大家问好

print('Hello Everbody!')

Hello Everbody!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值