01 variables
Variable is used to indicate or specify the name of a specific value, in Python variable assignment does not require type declaration, the assignment is created.
Variable naming rules:
1. The first letter or letters of the alphabet must underscore _ can not begin with a number
2. The remainder of the variables must consist of letters, numbers and underscore _ Composition
3. Variables are case sensitive
Use the equal sign (=) to assign values to variables. Equal sign (=) operator is a left variable name, an equal sign (=) operator is the right value stored in the variable. The figure is the number 666, 'data Phi Xia' are assigned to A, B variables.
Note: in python can be queried by the data structure type variable built-in function type ()
NOTE: A is numeric type, B is a string type
02 basic data structure type
There are six standard Python data structure type (extension package involve other types of data), the standard types of data structures are:
(1) Value Type
NOTE: A is a variable integer, B is a floating-point variables, C and D for the Boolean variables, E is a complex variable
(2) Type String
String enclosed in single quotation marks ( ') or double quotation mark ( "), and the string may use the plus (+) for splicing together, such as:
It is noted that when the intermediate string ends quotation marks also appeared, the need to add a backslash escape character before the intermediate quotation mark (\), such as:
Application: find the value of sin (Π / 2) and cos (Π / 3), as follows:
(3) the type of list
The list is written between square brackets ([]), by a comma (,) separated from the element, wherein the list can contain other type of data structure, as seen in FIG:
Square brackets (e.g., [0]) to the index values in the list, also possible to use square brackets (e.g. [2: 5]), taken in the form of a list of elements, the list of the index starting from 0, such as:
一般往列表中添加元素的函数为append(),使用方法如下:
修改元素用方括号索引的方式赋值,如:
删除元素直接用del函数删除索引出的元素,如:
(4)元组类型
元组有两种创建方法,一种是将一些值直接用逗号分开;另一种是用逗号分开并加上括号。注:元组不能像列表一样修改元素。如:
当元组只有一个值的时候要区分开元组和数值类型的不同,如代码中第一个为数值类型,第二个为元组类型。
(5)字典类型
字典由键和值组成,每个键与值用冒号(:)分隔,每一对键值对之间用逗号(,)分隔,同时将整个字典放在花括号内。如:
其他创建字典的方法:
(6)集合类型
使用大括号({ })或者set( )函数来创建集合,集合的作用很多时候可以用列表来代替,它的特点主要是可以用来删除重复元素,如下代码中1、2、3、4重复的元素只会出现一次:
03 数据类型相互转化
(1)数字与字符串的相互转化
(2)列表、元组、集合之间的相互转化
对Python感兴趣或者是正在学习的小伙伴,可以加入我们的Python学习扣qun:784758214,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地
(3)列表、元组、集合与字典的转化
将字典转化为列表、元组、集合只会保留其键,值不会保留,如: