Python variables and basic data types

1. Variables

a. What is a variable

Variable is the amount of change, and the core is the word "change" and "quantity", change is change, and quantity is the measure of state.

 

b. Why have variables

The essence of program execution is a series of state changes, and changes are the direct manifestation of program execution, so we need a mechanism that can reflect or save the state and state changes during program execution. #For example: The level of the hero is 1, and the monster upgrade (change) is 10. The survival status of the zombie is True, and it is killed by a plant, so it becomes False. The name of the person is egon, which can also be changed to Egon 

 

c. How to define variables

#变量名(相当于门牌号,指向值所在的空间),等号,变量值
name='Egon'
sex='male'
age=18
level=10

 

d. Definition of variables

1. 变量名只能是 字母、数字或下划线的任意组合
2. 变量名的第一个字符不能是数字
3. 关键字不能声明为变量名['and', 'as', 'assert', 'break', 
'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
'exec', 'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 
'raise', 'return', 'try', 'while', 'with', 'yield']

 

e. Definition:

#驼峰体
AgeOfOldboy = 56 
NumberOfStudents = 80
#下划线(推荐使用)
age_of_oldboy = 56 
number_of_students = 80

 

f. Bad way of defining variable names

#1. 变量名为中文、拼音
#2. 变量名过长
#3. 变量名词不达意

 

g. Defined variables will have: id, type, value

#1 等号比较的是value,
#2 is比较的是id

#强调:
#1. id相同,意味着type和value必定相同
#2. value相同type肯定相同,但id可能不同,如下
>>> x='Info Egon:18'
>>> y='Info Egon:18'
>>> id(x)
4376607152
>>> id(y)
4376607408
>>> 
>>> x == y
True
>>> x is y
False

 

h. Constants

常量即指不变的量,如pai 3.141592653..., 或在程序运行过程中不会改变的量
举例,假如老师的年龄会变,那这就是个变量,但在一些情况下,他的年龄不会变了,
那就是常量。在Python中没有一个专门的语法代表常量,程序员约定俗成用变量名全部
大写代表常量    AGE_OF_OLDBOY = 56
#ps:在c语言中有专门的常量定义语法,const int count = 60;一旦定义为常量,
更改即会报错

 

2. Basic data types

What is data? Why have multiple types of data?

The data is the value of the variable, such as age=18, 18 is the data we save.

Variables are used to reflect/maintain state and state changes. There is no doubt that different types of data should be used to identify different states.

 

a. Numbers

#int整型
定义:age=10 #age=int(10)
用于标识:年龄,等级,身份证号,qq号,个数
#float浮点型
定义:salary=3.1 #salary=float(3.1)
用于标识:工资,身高,体重,

 

b. String

In python, the quoted character is a string type, python does not have a character type.

Definition: name='egon' #name=str('egon') 

Used to identify: Descriptive content such as name, gender, nationality, race

What's the difference between single quotes, double quotes, and multiple quotes?

#那单引号、双引号、多引号有什么区别呢? 让我大声告诉你,单双引号木有任何区别,只有下面这种情况 你需要考虑单双的配合
msg = "My name is Egon , I'm 18 years old!"

#多引号什么作用呢?作用就是多行字符串必须用多引号
msg = '''
今天我想写首小诗,
歌颂我的同桌,
你看他那乌黑的短发,
好像一只炸毛鸡。
'''
print(msg)

 

String concatenation (can only be done between strings, and can only be added or multiplied)

#数字可以进行加减乘除等运算,字符串呢?让我大声告诉你,也能?what ?是的,但只能进行"相加"和"相乘"运算。
>>> name='egon'
>>> age='18'
>>> name+age #相加其实就是简单拼接
'egon18'
>>> name*5 
'egonegonegonegonegon'

#注意1:字符串相加的效率不高
字符串1+字符串3,并不会在字符串1的基础上加字符串2,而是申请一个全新的内存空间存入字符串1和字符串3,相当字符串1与字符串3的空间被复制了一次,
#注意2:只能字符串加字符串,不能字符串加其他类型
字符串拼接(只能在字符串之间进行,且只能相加或相乘)

 

3. List

Separated by commas in [], you can store n value definitions of any type:

students=['egon','alex','wupeiqi',] #students=list(['egon','alex','wupeiqi',]) 

Used for identification: situations where multiple values ​​are stored, such as a person with multiple hobbies

#存放多个学生的信息:姓名,年龄,爱好
>>> students_info=[['egon',18,['play',]],['alex',18,['play','sleep']]]
>>> students_info[0][2][0] #取出第一个学生的第一个爱好
'play'

 

4. Dictionary

#为何还要用字典?
存放一个人的信息:姓名,性别,年龄,很明显是多个值,
既然是存多个值,我们完全可以基于刚刚学习的列表去存放,如下
>>> info=['egon','male',18]
定义列表的目的不单单是为了存,还要考虑取值,如果我想取出这个人的年龄,可以用
>>> info[2]
但这是基于我们已经知道在第3个位置存放的是年龄的前提下,我们才知道索引2对应的是年龄
即: #name, sex, age
info=['egon','male',18]
而这完全只是一种假设,并没有真正意义上规定第三个位置存放的是年龄,于是我们需要寻求
一种,即可以存放多个任意类型的值,又可以硬性规定值的映射关系的类型,比如key=value,这就用到了字典

 

在{}内用逗号分隔,可以存放多个key:value的值,value可以是任意类型定义:info={'name':'egon','age':18,'sex':18} info=dict({'name':'egon','age':18,'sex':18})用于标识:存储多个值的情况,每个值都有唯一一个对应的key,可以更为方便高效地取值

info={
   'name':'egon',
   'hobbies':['play','sleep'],
   'company_info':{
       'name':'Oldboy',
       'type':'education',
       'emp_num':40,
   }
}
print(info['company_info']['name']) #取公司名
students=[
   {'name':'alex','age':38,'hobbies':['play','sleep']},
   {'name':'egon','age':18,'hobbies':['read','sleep']},
   {'name':'wupeiqi','age':58,'hobbies':['music','read','sleep']},
]
print(students[1]['hobbies'][1]) #取第二个学生的第二个爱好

字典相关的嵌套、取值

 

5.布尔

#布尔值,一个True一个False
#计算机俗称电脑,即我们编写程序让计算机运行时,应该是让计算机无限接近人脑,
或者说人脑能干什么,计算机就应该能干什么,人脑的主要作用是数据运行与逻辑运算,
此处的布尔类型就模拟人的逻辑运行,即判断一个条件成立时,用True标识,不成立则用False标识
>>> a=3
>>> b=5
>>> 
>>> a > b #不成立就是False,即假
False
>>> 
>>> a < b #成立就是True, 即真
True

接下来就可以根据条件结果来干不同的事情了:
if a > b 
  print(a is bigger than b )
else 
  print(a is smaller than b )
上面是伪代码,但意味着, 计算机已经可以像人脑一样根据判断结果不同,来执行不同的动作。

布尔类型的重点知识!!!:

所有数据类型都自带布尔值

None,0,空(空字符串,空列表,空字典等)三种情况下布尔值为False ,其余均为真 

 

6.元组

Python的元组与列表类似,不同之处在于元组的元素不能修改。

元组使用小括号,列表使用方括号。

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

如下实例:

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
创建空元组
tup1 = ();
元组中只包含一个元素时,需要在元素后面添加逗号
tup1 = (50,);

 

 


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326157452&siteId=291194637