1. Variables: Variables are a part of the computer's memory, and the variables can store values within the specified range, and the values can be changed.
Variable name: variable names are composed of letters, numbers, and underscores;
Numbers cannot start, or pure numbers;
Keywords cannot be used;
Example: Assignment of a A1 a_ a_1 variable: A = 1 example:>>> a=2 >>> a 2
#数字不可以开头: syntaxerror:invalid syntax syntax error;
2, the variable is a label
>>> ID (a) 7989040>>> a=121221>>> ID (a) 7989136
#id () view memory address; #变量是对赋值的一个引用; #同样的一个地址空间, you can have multiple labels;>>> b=1>>> c=1>>> ID (b) 7713592>>> ID (c) 7713592
3. Assignment Operation
The Pthon operator includes assignment operations | arithmetic operations | relational operations | logical operations;
An expression is a formula that links different data (including variables, functions) to a certain rule by using operational symbols.
Arithmetic operators:
+ Plus-minus * multiply/real number divide//integer division% seek power operation
Example: >>> 1+12>>>4-13>>>3/21>>>3.0/21.5>>> 3//21>>> 3.0//21.0
>>> B = 3 * * 3 #赋值运算 >>> b27>>> print b27>>> b-2 25>>> a-=50 #赋值给自己 >&G T;> A50
Relational operators
#判断的作用 >>> 1<2true>>> 1>2false>>> 3>5false>>> 3!=5 #注意! = Order True
logical operators
and Logic and # Two conditions are true at the same time, return True
The or logical or # Two condition fails at the same time, returning false
Not logical no # judgment result reversed
>>> 1>2false>>> not 1>2true>>> not 6<2 or 4<2 True
#运算符的优先级
Simple arithmetic operations and data laws are consistent.
A=2+4*4
Assignment operations
High and assignment arithmetic of numerical operators
#计算器
#!/usr/bin/pythona = Int (raw_input ("Please input number1:")) b = Int (raw_input ("Plwase input number2:")) print A+bprint A-bprint a*bprint A%b#raw_input () method #int () Define numeric types, strings cannot be added
4. Data type
#不需要事先申明, according to the characteristics of things
Digital
integer int: range-2147 483 648 to 2147 483 647
>>> num1=123>>> ID (NUM1) 21892896>>> type (NUM1) <type ' int ' >
Long: A large range, in order to distinguish between ordinary integers and long integers, you need to add L or lowercase l after an integer
#人为定义长整型
>>> num1=2l>>> Type (NUM1) <type ' Long ' > #存的值只有2 # We recommend using uppercase L
floating point type : (decimal type) flote
For example: 0.0, 12.0,-18.8, 3e+7>>> x=3e+7>>> type (x) <type ' float ' >>>> y=0.0>>> Type (y) <type ' float ' >>>> a=1.0>>> type (a) <type ' float ' > #如果想要变为浮点型, only need to add a decimal point
plural type:complex |python provides inline support for complex numbers, which is not available in most other software
Example: 3.14j, 8.32e-36j>>> num1=3.145j>>> type (NUM1) <type ' complex ' >>>> #小写字母j Represents the definition of a complex type; here the e=10
###################### #123 and "123" #########################################
>>> a=123>>> stra= "123" >>> print a123>>> print stra123>>> A + Stratraceback (most recent): File ' <stdin> ', line 1, in <module>typeerror:unsupported operand type (s) for +: ' int ' and ' str ' >>> type (stra) <type ' str ' >
#######################################################################
Strings string
Collection of #使用引号定义的一组可以包含数字, letters, symbols (non-special system symbols)
#三重引号 docsting
>>> str1= ' Hello word ' >>> str2= "Hello good" >>> str3= "I ' m is Kong" >>> type (STR3) < Type ' str ' > #单引号和双引号没有任何区别, differentiated treatment (e.g. let's Go) #如果更复杂可以使用转义符号 \ (e.g.:>>> say= "let's \" Go\ ") #控制字符串换行, need to use escape character NewLine \n>>> mail= "Hello \ n My name is Kong" >>> print Mailhello my name is Kong
# "" " Triple quote
>>> mail= "" "Tom: ... I am jack ... good bay ... "" ">>> Mail ' tom:\n\ti am jack\n\tgood bay\n ' >>> print mailtom:i am Jack Good bay>>>
############### a string, list, or tuple in a #在python is called a sequence type of data #######################
string:
Example:>>> a= "abced" >>> a[0] ' a ' >>> a[1] ' B ' >>> a[0]+a[1] ' ab '
#+ two strings in a stitch
#起始索引从0开始, but it only takes one
#切片
>>> A[:2] ' ab ' >>> a[4:] ' e '
Tuple tuples:
Tuples and lists are very similar, except that tuples and strings are immutable , that is, tuples cannot be modified.
Tuples are defined by a comma -delimited list of items in parentheses
Tuples are typically used when a statement or user-defined function can safely take a set of values, that is, the value of the tuple being used does not change.
Tuple Creation Example:
Generic tuple
>>> t= ("name", "Kong", "age", "all") >>> t[1] ' Kong ' >>> define single tuple >>> cc= ("My Name Kong",) >>> Cc[3] #注意要添加一个逗号 # defines an empty tuple consisting of a pair of empty parentheses
Operation of tuples
tuples and string types are part of a sequence type and can be manipulated by indexing and slicing.
Extra action: Define a variable, split the tuple (the pattern that defines the variable)
>>> t= ("Milo", "+", "male") >>> name,age,gender=t>>> name ' Milo ' similar to >>> a,b,c= 1,2,3>>> a1>>> b2>>> a,b,c= (square) Note add parentheses >>> t=["name", "Age"]>>> t1=[" Kong "," Age "]>>> zip (t,t1) [(' Name ', ' Kong '), (' Age ', ' age ')] #zip遍历, into tuples, but no absolute contact
List ""
#list is the data structure that handles a set of ordered items, that is, you can store a sequence of items in a list
Lists are mutable types of data
The list consists of a list with [] that contains multiple comma-separated numbers, or a string
Definition of empty list:
>>> list1=[]>>> Type (list1) <type ' list ' > a single worthy definition does not require a comma, but also a list (tuples need to have commas) >>> list1=[ 2,3,4,]>>> list1[2, 3, 4]>>> list1=["Milo", "a", "male",]>>> List1[0] # Take the same value as the tuple, The only difference is that you can re-assign the value ' Milo '
List of actions:
>>> list1[' Kong ', ' a ', ' Male ']>>> ID (list1) 140548135398648>>> list1[0]= "Kon" >> > ID (list1) 140548135398648 Note # storage space is not modified, but tuples are not possible
Add:
#借助列表类型的方法 >>> list1[' kon ', ' a ', ' Male ', ' 12345678 ']>>> list1.append (' phone ') >>> list1[' Kon ', ' a ', ' Male ', ' 12345678 ', ' phone ']
Delete:
List1.remove ("12345678") #如果有多个, it will select the first culling or: List1.remove (list1[3]) #注意list1是一个变量, list name to manipulate [' Kongjiagou ', ' 1233e2e32 ']>>> del (mail[0])
two main features of the sequence:
Index operators: Fetching specific items from a sequence
Slice operator: Gets a slice of a sequence that is part of a sequence
basic operation of the sequence:
1, Len (): To find the sequence length
2, +: Connect two sequences
3, *: Repeating sequence element "#" *50
4, in: Determine if the element is in sequence
>>> str1= "Abcdefssfs" >>> ' C ' in str1true# note the use of quotes, which is a way to differentiate between strings and variables >>> str1= "abcded" >> > ' A ' in str1true>>> a=100>>> ' a ' in str1true>>> A in str1traceback (most recent call last): File "<stdin>", line 1, in <module>typeerror: ' In <string> ' requires string as left operand, not INT&G T;>>
5, Max (): Returns the maximum value
6, MIN (): Returns the minimum value
7. CMP (TUPLE1,TUPLE2) compares two sequence values
#############################################################################################
Dictionary :
#存储数据的方式, more convenient
Dictionary is the only type of mapping in Python (hash table # unordered)
The Dictionary object is mutable, but the key of the dictionary must use the immutable object, and different types of health values can be used in a dictionary.
Keys () or values () returns a list of key or value
Items () returns a tuple containing a health value pair
Create a dictionary using {}
>>> dic={"name": "Kong", "Age": "232"}>>> dic["name"] ' Kong ' mixed type definition >>> dic1={1: ' Name ', ' age ' ': ', ' d ': ' 1232 '}>>> dic1[1] ' name '
#工厂方法dict () generates a dictionary, but it is slow, inefficient, another way is Fromkeys (), the elements in the dictionary have the same value, default to None, the most common or {}
#访问字典的值: can be accessed directly using key: Key does not exist will be error, can use Had_key () or in and not to judge, but the Has_key () method is about to be discarded.
For loop traversal:
>>> >>> dic1{1: ' Name ', ' Age ':-' d ': ' 1232 '}>>> for K in Dic1: ... print k ... 1aged>>> for k in Dic1: ... dic1[k] ... ' Name ' 20 ' 1232 '
This article from "thinking More than technology" blog, declined reprint!
Basic Python Learning 1