Note: For me this used to be in C + + students, may start learning pyhon a bit not adapt to, why? Because, in Python, there's no such thing: {}, you don't have to separate each sentence with semicolons. In Python, you can make a sentence by indenting it in chunks. The following study is based on python2.7
From __future__import Division
Start, while learning to make notes ha. hahaha, try to finish it in two days.
Basic input and Output statements print and Raw_input ():
# Input statement: Print ' 100+100=%d ' % (100+100)100+100=200# output statement >>> name = raw_input ('input Your name:') input your name:yinheyiprint('hello,%s ' %name) hello,yinheyi
Note: After the raw_input () statement is received, it is saved by default to str format Oh, if you want to compare size, remember to convert to int type.
>>> number = Raw_input ('input a number:') input a number:12>>> type (number) ' Str '>>>> number = Int (raw_input ('input a number:') ') input a number :12>>> Type (number)'int'>
Judging and looping statements:
Judgment statement: If...elif...else
if a>=: ... Print ' The number is big ' elif A < : ... Print ' The number is small ' else : ... Print " I don ' t know " is big
Looping statements: for...in
>>> sum = 0 for in range (101): ... = sum +print sum5050
Loop statement: While
>>> sum = 0 while n < 101 : ... = sum + n ... = n + 1print sum5050
Data type: list, tuple, dictionary, set
A data type built into the List:python is a list. Lists is an ordered set of elements that can be added and deleted.
To generate it is to use [];
When it is called, it can be called with the subscript (starting from 0), such as the first element, list[0]; the penultimate, the list[-1];
You can use the Len () function to get the number of list elements;
Add the tail with append (), insert with insert (), trailing delete with pop (), and the specified position to pop (i);
#define a list of friends>>> friends = ['xiaoming','Ergou','Sanmao']>>>friends['xiaoming','Ergou','Sanmao']>>>Friends[0]'xiaoming'>>> friends[-1]'Sanmao'#Add and remove>>> Friends.append ('Dapeng')>>>friends['xiaoming','Ergou','Sanmao','Dapeng']>>> Friends.insert (0,'Diyi')>>>friends['Diyi','xiaoming','Ergou','Sanmao','Dapeng']>>>Friends.pop ()'Dapeng'>>>friends['Diyi','xiaoming','Ergou','Sanmao']>>> Friends.pop (2)'Ergou'>>>friends['Diyi','xiaoming','Sanmao']#Replace>>> Friends[0] ='Wangbadan'>>>friends['Wangbadan','xiaoming','Sanmao']
Tuple (tuple): Tuple and list are very similar, but the tuple cannot be modified once initialized. Just remember that it can't be modified. It's safe.
Define with ().
Call with subscript, i.e. tuple[1];
Note: When it defines a tuple of an element, it must be written like this: name = (' Yin ',), never write name = (' Yin ');
Name = ('zhai','Yin')>>> name ( ' Zhai ' ' Yin ' )>>> name[0]'zhai'>>> name[-1] 'Yin'
Dictionary (dictionary): It is a key-value pair. As a container for map in the C + + language. It is characterized by the ability to quickly find, need to occupy a lot of memory, memory waste much. The algorithm for calculating the position by key is called the hash Algorithm (hash).
Use {} to define dictionary oh;
As the dictionary increases, the lookup time does not increase.
Add value to a key multiple times, and the value below will flush the previous value out:
You can use the ' key ' in dic or dic.get (' key ') method to see if the key exists. Note: The Get method provided by Dict, if key does not exist, can return none, or its own specified value, returns none when the interactive command line of Python does not display the result.
Delete with: Pop (key). When added, it is possible to add directly to the index of the key value.
Note: The order in which the internal storage of ICT is stored is not related to the order in which key is placed.
#define a dictionary;Score = {'xiaoming': 85,'Xiaohong': 62}>>>score{'xiaoming': 85,'Xiaohong': 62}#add a single element;>>> score['Wangdan'] = 85>>>score{'xiaoming': 85,'Xiaohong': 62,'Wangdan': 85}#delete an element;>>> Score.pop ('Xiaohong')62>>>score{'xiaoming': 85,'Wangdan': 85}#see if key exists;>>>'xiaoming' inchscoretrue#view the value corresponding to a key;>>> Score.get ('Wangdan') returns a custom value when it does not exist;85>>> Score.get ('hehehe',-1)-1
Set:set and dict are similar, but also a set of keys, but do not store value. Because key cannot be duplicated, there is no duplicate key in set. The only difference between set and dict is that it does not store the corresponding value, but the principle of set is the same as dict, so it is also not possible to put mutable objects.
To create a set, you need to provide a list as the input collection:
Add and remove elements via Add (key) with remove (key);
>>> s = Set ([1, 2, 3])>>> sset ([1, 2, 3])
Note that the passed in parameter [1, 2, 3] is a list, and the display set([1, 2, 3]) only tells you that this set has 3 elements in the inside of the "X", which shows [] does not indicate that this is a list.
Repeating elements are automatically filtered in set:
>>> s = Set ([1, 1, 2, 2, 3, 3])>>> sset ([1, 2, 3])
Add and remove
>>> s = Set ([All])>>>Sset ([1, 2, 3])>>> S.add ('xiaoming')>>>Sset ([1, 2, 3,'xiaoming'])>>>s.add (0)>>>Sset ([0,1, 2, 3,'xiaoming'])>>> S.remove (2)>>>Sset ([0,1, 3,'xiaoming'])
The most basic syntax in Python (1)