目录

2.1 变量

2.2 字符串

1.修改字符串

2.合并字符串

3.制表符/换行符

4.删除空白

2.3 数字

1.整数

2.浮点数

2.4 注释

2.5 Python之禅

作业


2.1 变量

定义变量

variable x="XXXX"

变量遵从以下规则:

(1)只能包含字母、数字及下划线,且不能以数字开头;

(2)变量名不可包含空格

(3)尽量使用小写字母命名变量

>>> variable="hello world"
>>> print(variable)
hello world
>>> variable="hello china"
>>> print(variable)
hello china

2.2 字符串

用单/双引号括起的即是字符串

eg."I am your classmate"

'I like water'

1.修改字符串

(1)title( )以首字母大写形式显示每一个单词

>>> variable="i am your classmate"
>>> print(variable.title())
I Am Your Classmate

(2)upper( )以大写形式显示每一个单词

>>> print(variable.upper())
I AM YOUR CLASSMATE

(3)lower( )以小写形式显示每一个单词

>>> print(variable.lower())
i am your classmate

2.合并字符串

+合并字符串

>>> xing="tian"
>>> ming="da ben"
>>> xing_ming=xing+" "+ming
>>> print(xing_ming)
tian da ben

3.制表符/换行符

(1)制表符\t

>>> print("python")
python

>>> print("\t python")   #\t后可不加空格
         python

(2)换行符\n

>>> print("wo shi xiao bai tu")
wo shi xiao bai tu
>>> print("wo \n shi \n xiao \n bai \n tu")  #\n前后可不加空格
wo
 shi
 xiao
 bai
 tu

4.删除空白

(1)删除末尾空白 rstrip( )

(2)删除开头空白 lstrip( )

(3)删除两端空白 strip( )

2.3 数字

1.整数

(1)基础运算:+ - * /

(2)次方运算:**

2.浮点数

带小数点的数字即浮点数

str( )将非字符串值表示为字符串

>>> age=23
>>> print("Happy "+age +"day")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print("Happy "+str(age)+" day")
Happy 23 day

2.4 注释

matlab中使用%,与之不同的是,python使用#作为注释符

2.5 Python之禅

>>> 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!
>>>

作业

1.将一条消息存储到变量中,再将其打印出来。

>>> message="hello my name is by4te"
>>> print(message)
hello my name is by4te

2.将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。

>>> message="hello my name is by4te"
>>> print(message)
hello my name is by4te
>>> message="who are you"
>>> print(message)
who are you

3.将用户的姓名存到一个变量中,并向该用户显示一条消息。

>>>name="curry"
>>> print("Hello "+ name +",I am by4te")
Hello curry,I am by4te

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

>>> name="curry"
>>> print(name.upper())
CURRY
>>> print(name.lower())
curry
>>> print(name.title())
Curry


5.找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。


(Albert Einstein oncesaid,“Apersonwho never madea mistake never tried anything new.” )



>>> name="philips"
>>> print(name.title()+" once said:Failure is the mother of success")
Philips once said:Failure is the mother of success


将名人的姓名存储在变量 famous_person 中,再创建要显示的消息,并将其存储在变量 message 中,然后打印这条消息


>>> famous_person="philips"
>>> message="failure is the mother of success"
>>> print(famous_person+" "+message)
philips failure is the mother of success

7.存储一个人名,并在其开头和末尾都包含一些空白字符。务必至少使用字符组合"\t" 和"\n" 各一次。 打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数lstrip() 、rstrip() 和strip() 对人名进行处理,并将结果打印出来。

>>> name
' curry '
>>> print(name.lstrip())
curry
>>> print(name.rstrip())
 curry
>>> print(name.strip())
curry

8.编写4个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字8。

>>> print(2+6)
8
>>> print(10-2)
8
>>> print(2*4)
8
>>> print(8/1)
8.0

9.将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来。

>>> number=6
>>> print("my favorite number is "+str(number))
my favorite number is 6

10.选择你编写的两个程序,在每个程序中都至少添加一条注释。

答:#

11.在Python终端会话中执行命令import this ,并粗略地浏览一下其他的指导原则。

答:略