Datatype:
float — Floating point numbers can be accurate to the back of the decimal point.15classifier for honorific people int — Integer can be infinite bool — nonzerotrue,zero signfalse list — listings
Float/Int:
Operators:
/ - Floating point division
// - rounding when the result is positive; 11/5 = 2; 11/4 = 2
When the result is negative, round down; -11//5=-3; -11/4=-3
When the numerator and denominator are both float, the result is float.
** - calculate the power; 11**2 = 121
% - take the balance
Other mathematical operations:
1. Score:
import fractions;
(1,3) — 1/3
import math;
—()
—()
—()
—()
—3.1415926…
(/2) — 1.0
(/4) — 0.9999999999…
(); math
List:
Created: a_list = ['a', 'b', 'mpilgrim', 'z', 'example']
a_list[-1] — ‘example'
a_list[0] — ‘a'
a_list[1:3] — [‘b', ‘mpilgrim', ‘z']
a_list[:3] — [‘a', ‘b', ‘mpilgrim' ]
a_list[3:] — [‘z', ‘example']
a_list[:]/a_list — [‘a', ‘b', ‘mpilgrim', ‘z', ‘example']
*Note: a_list[:] and a_list return different lists, but they have the same elements!
a_list[x:y] - Get the list slices, x specifies the start position of the first slice index, y specifies the position of the index of the cutoff but not included slice.
Adds elements to the list:
a_list = [‘a']
a_list = a_list + [2.0, 3] — [‘a', 2.0, 3]
a_list.append(True) — [‘a', 2.0, 3, True]
a_list.extend([‘four','Ω']) — [‘a', 2.0, 3, True,'four','Ω']
a_list.insert(0,'Ω') — [‘Ω','a', 2.0, 3, True,'four','Ω']
list other functions:
a_list = [‘a', ‘b', ‘new', ‘mpilgrim', ‘new']
a_list.count(‘new') — 2
a_list.count(‘mpilgrim') — 1
‘new' in a_list — True
a_list.index(‘new') — 2
a_list.index(‘mpilgrim') — 3
a_list.index(‘c') — through a exception because ‘c' is not in a_list.
del a_list[1] — [‘a', ‘new', ‘mpilgrim', ‘new']
a_list.remove(‘new') — [‘a', mpilgrim', ‘new']
Note: remove only removes the first 'new'
a_list.pop() - 'new'/['a', mpilgrim'] (delete and return the last element)
a_list.pop(0) - 'a' / ['mpilgrim'] (removes and returns the 0th element)
Empty lists are false, other lists are true.
tuple (elements are immutable lists):
Definition: the same as for lists, except that the entire set of elements is enclosed in parentheses, not square brackets.
a_tuple = (“a”, “b”, “mpilgrim”, “z”, “example”)
a_tuple = (‘a', ‘b', ‘mpilgrim', ‘z', ‘example')
tuple can only be indexed, not modified.
Advantages of tuples over lists:
1. Fast
2. "Write-protect" for more security
3. some tuples can be used as dictionary keys?
The built-in tuple() function takes a list argument and converts the list into a tuple.
Similarly, the list() function converts a tuple into a list
Assign multiple values at the same time:
v = (‘a',2, True)
(x,y,z) = v — x=‘a', y=2, z=True
range() - built-in function to assign values to consecutive variables
(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) = range(7)
Monday — 0
Thursday — 3
Sunday — 6
range() - The built-in function range() constructs a sequence of integers, and range() returns an iterator.
collection (the values inside are unordered):
Create a collection: separate each value with a comma and include all values with curly braces{}.
a_set = {1}
type(a_set) — <class ‘set'>
Create collections based on lists:
a_list = [‘a', ‘b', ‘mpilgrim', True, False, 42]
a_set = set(a_list)
a_set — {‘a', ‘b', ‘mpilgrim', True, False, 42}
a_set = set() - gets an empty set
a_dic = {} - get an empty dic
Modify the collection:
a_set = {1,2}
a_set.add(4) — {1,2,4}
len(a_set) — 3
a_set.add(1) — {1,2,4}
a_set.update({2,4,6}) — {1,2,4,6}
a_set.update({3,6,9}, {1,2,3,5,8,13}) — {1,2,3,4,5,6,8,9,13}
a_set.update([15,16]) — {1,2,3,4,5,6,8,9,13,15,16}
a_set.discard(16) — {1,2,3,4,5,6,8,9,13,15}
a_set.discard(16) — {1,2,3,4,5,6,8,9,13,15}
a_set.remove(15) —{1,2,3,4,5,6,8,9,13}
a_set.remove(15) — through a exception
a_set.pop() — return 1 / {2,3,4,5,6,8,9,13}
Note: a_set.pop() randomly deletes a value in the set and returns that value.
a_set.clear() — set()
a_set.pop() — through exception.
Other operations on sets:
a_set = {2,3,4,5,6,8,9,13}
30 in a_set — False
4 in a_set — True
b_set = {3,4,10,12}
a_set.union(b_set) - union of two sets
a_set.intersetion(b_set) - intersection of two sets
a_set.difference(b_set) - elements that are in a_set but not in b_set
a_set.symmetric_difference(b_set) - returns all elements that appear in only one set
a_set.issubset(b_set) - determine if a_set is a subset of b_set
b_set.issuperset(a_set) - determine if b_set is a superset of a_set
In a Boolean type context environment, the empty set is false and any set containing more than one element is true.
Dictionary (unordered collection of key-value pairs):
Create a dictionary:
a_dic = {‘server':'db.',
‘databas':'mysql'}
a_dic[‘server'] — ‘db.'
a_dic[‘database'] — ‘mysql'
Modify the dictionary:
a_dic[‘user'] = ‘mark' — {'user': 'mark', 'server': 'db.', 'database': ‘blog'}
a_dic[‘database'] = ‘blog' — {'user': 'mark', 'server': 'db.', 'database': ‘blog'}
a_dic[‘user'] = ‘bob' — {'user': 'bob', 'server': 'db.', 'database': ‘blog'}
a_dic[‘User'] = ‘mark' — {'user': 'bob', ‘Uuser': 'mark', 'server': 'db.', 'database': ‘blog'}
Note: 1. Duplicate keys are not allowed in the dictionary. Assigning a value to an existing key will overwrite the original value;
2. New key-value pairs can be added at any time;
3. Dictionary keys are case sensitive.
Mixed-value dictionary:
suffixes = { 1000:[‘KB', ‘MB', ‘GB', ‘TB', ‘PB', ‘EB', ‘ZB', ‘YB'],
1024: [‘KiB', ‘MiB', ‘GiB', ‘TiB', ‘PiB' , ‘EiB', ‘ZiB', ‘YiB']}
len(suffixes) — 2
1000 in suffixes — True
suffixes[1024] — [‘KiB', ‘MiB', ‘GiB', ‘TiB', ‘PiB' , ‘EiB', ‘ZiB', ‘YiB']
suffixes[1000][3] — ‘TB'
Empty dictionary is false, all other dictionaries are true.
The above mentioned is the whole content of this article, I hope you will enjoy it.