200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 【Python编程入门】Lecture 2:变量和简单数据类型

【Python编程入门】Lecture 2:变量和简单数据类型

时间:2019-11-29 15:43:52

相关推荐

【Python编程入门】Lecture 2:变量和简单数据类型

2 变量和简单数据类型

2.1 Hello Python world!2.2 变量2.2.1 变量的命名规则2.2.2 变量的使用2.2.3 同时给多个变量赋值2.2.4 常量 2.3 字符串2.3.1 修改大小写的方法2.3.2 f字符串(format方法)2.3.3 制表符与换行符2.3.4 删除首尾空白 2.4 整数与浮点数2.4.1 算术运算2.4.2 浮点数的处理2.4.3 数中的下划线 2.5 注释2.6 Python之禅

假设现在我们已经搭建好了Python环境,开始进一步学习Python语言。

2.1 Hello Python world!

此时,你需要编写第一个.py程序,运行Anaconda,启动Spyder(或Jupyter Notebook等),在File菜单选择“Save as”,将程序保存在桌面新建的“python_work”文件夹,文件名为“hello_world.py”。

保存后,在文件中输入如下代码:

print("Hello Python world!")

在Run菜单中选择“Run”,则在Spyder右下角出现输出结果

runfile('C:/Users/xxx/Desktop/python_work/hello_world.py', wdir='C:/Users/xxx/Desktop/python_work')Hello Python world!

其中runfile是执行文件的意思,不必理会。

2.2 变量

可以在hello_world.py中使用一个变量,我们需要对代码进行如下修改

message = "Hello Python world!"print(message)

运行该程序,可以得到与之前相同的输出结果。

在这个程序中,我们添加了一个名为message的变量,每一个变量都指向一个值,即与该变量关联的信息,在这里message指向字符串"Hello Python world!"。

2.2.1 变量的命名规则

1、变量名只能包含字母、数字、下划线,不能包含空格,可以使用下划线代替空格。(greeting message含有空格)

2、变量名只能以字母或下划线打头,不能以数字打头。(1_message以数字打头)

3、不要使用Python关键字和函数名作为变量名。(print是Python既有的函数名)

4、变量名应当既简短又有描述性。(student_name比s_n更适合作为变量名)

5、慎用小写l和大写O,因为他们很像1和0。

6、一般只使用小写作为变量名。

2.2.2 变量的使用

变量是一种可以赋值的标签,它指向特定的值。

使用变量时注意避免错误,如因为字母敲错导致拼写的结果与创建的变量不一致,则会报错。

2.2.3 同时给多个变量赋值

x,y,z = 0,0,0

2.2.4 常量

常量类似于变量,但是其在程序的整个生命周期内保持不变。Python没有内置的常量类型,但程序员一般使用全大写指出某个值应该为常量,其值应该始终不变。

CONSTANT = 100

2.3 字符串

字符串就是一系列字符,在Python中,用单引号双引号括起来的都是字符串,这种灵活性能够让我们在字符串中包含引号和撇号,同时使用时要注意避免歧义造成语法错误。

"This is a string"'This is also a string'

2.3.1 修改大小写的方法

方法是Python可对数据执行的操作,通过“.”来调用。

name = "WEI tong"print(name.title())print(name)print(name.upper())print(name)print(name.lower())print(name)

运行结果

Wei TongWEI tongWEI TONGWEI tongwei tongWEI tong

1、.title()对每个首字母大写

2、.upper()对所有字母大写

3、.lower()对所有字母小写

注意:这三个方法都不改变原字符串。

2.3.2 f字符串(format方法)

f字符串可用于合并两个字符串。

first_name = "WEI"last_name = "Tong"full_name = f"{first_name} {last_name}"message = f"Hello {full_name.title()}, would you like to learn some Python today?"print(message)

运行结果

Hello Wei Tong, would you like to learn some Python today?

注意,对于Python3.5及之前的版本,要使用format()方法

full_name = "{}{}".format(first_name,last_name)

2.3.3 制表符与换行符

1、制表符

\t可以插入一个制表符

print("Python")print("\tPython")

PythonPython

2、换行符

\n可以插入一个换行符

print("Python\nC++")

PythonC++

2.3.4 删除首尾空白

name = " wei t\tong \n tong "print(name)print(name.strip())print(name.lstrip())print(name.rstrip())

wei t ong tong wei t ong tongwei t ong tong wei t ong tong

1、.rstrip()删除左侧空白

2、.lstrip()删除右侧空白

3、.strip()删除两侧空白

注意:这三个方法都不改变原字符串,该操作只针对字符串首位的空白。

2.4 整数与浮点数

2.4.1 算术运算

Python中的算术运算符

加(+)

减(-)

乘(*)

除(/)

乘方(**)

求整数商(//)

取模(%)

注意:

1、求整数商指的是向下取整;

2、取模的结果永远出现在最小非负完全剩余系中;

这两句话很抽象,只是想说明对于负数而言,Python中的取模运算和数学上常见的取余还是有一定的区别的,举个例子:

In[1]: -8%3Out[1]: 1In[2]: -8//3Out[2]: -3

在Python中(取模),-8÷3=-3……1

但实际上在数学上(取余),-8÷3=-2……-2

(3的最小非负完全剩余系为0,1,2,)

Python将所有带小数点的数称为浮点数

注意:

1、任意两个数相除结果总是浮点数。

2、只要操作数是浮点数,Python默认得到的结果总是浮点数。

2.4.2 浮点数的处理

一般地讲,使用浮点数时无需考虑其行为,Python通常会按期望的方式处理它们。但是需要注意的是,结果包含的小数位数可能是不确定的,这是所有编程语言都存在的问题。

2.4.3 数中的下划线

书写很大的数时,可以使用下划线将其中的数字分组。在Python看来,1000和1_000是没有区别的,Python在存储这种数时会忽略其中的下划线。

2.5 注释

Python中利用#标识注释。

#这是一条注释

2.6 Python之禅

import this

The Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than plex 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!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。