Note 1
标签:python
目录learning techniques
take python as a calculator and build the whole architecture from this base gradually.
1.format
1.1 comment
### single line comment
''' ''' multiple lines comment 1
""" """ multiple lines comment 2
1.2 line and indence
Different from Java and CPP does,python won't use curly brace but indence instead.The adjoining code blocks with the same priority should have same indence.
if a > 1 :
b = 1
c = 2
2.number
2.1 how many types of number does a methematical calculator may have?
(Python accepts these types of input.But you should understand how these numbers are stored into device.)
- integer
- float number
- fraction
- complex number(real number + imaingary number)
- bool
I: Integer is categorized into int and long int labelled by L most time and its lower case 'l' hardly \(\to\):eschewing the ambiguity between l and 1(:number 1).
II: boolean variables and some similar functions:type(),isinstance(),issubclass()
# 1 acts as boolean variable -> True
if 1 :
print ("1 can also be \"True\" ")
else :
print ("1 cannot also be \"True\" ")
# True can add False as well as numbers
print(True + False + 0)
#function type()
bool1 = (1==1)
print(type(bool1))
#Given B extends A
#type(B) won't produce A
#but isinstance(B,A) produces True
print(isinstance(bool1,int))
#judge whethere parameter 1 is the child class of parameter 2
print(issubclass(bool,int))
III: complex(real number,imaginary number):
#produce 12 + 34j
print(complex(12,34))
3.string
(stretch from number to char and string initially with an assumption of only a calculator )
3.1 representation
enclosed by single quote '' or double quote ""
Example: 'hello' "world!"
3.2 index
Given a = "python"
start from the beginning with the index 0 and step forward with one step increased:
0 -> length-1 : a[0] == 'p' and a[5] == 'n'
start from the end with the index -1 and step backward with one step decreased:
-1 - length - 1 -> -1 : a[-6] == 'p' and a[-1] == 'n'
Note:(ruminate this snippet)
a = "HelloWorld!"
a[3] = 'X'
Terminal shell prompts:
Traceback (most recent call last):
File "c:\Users\ASUS\test.py", line 5, in <module>
a[3] = 'X'
TypeError: 'str' object does not support item assignment
\(\to\)the re-assignment to one element in a string is forbidden.
3.3 segment(as dafny does)
Given a = "python"
#will produce "on" from position 4 to 5(position 6 is exclusive).
print(a[4:6])
#will produce "python" from the beginning to the end
print(a[:])
print(a[0:])
print(a[:6])
4.operator
a mathematical calculator,yet more often than not,has
- addition
- subtraction
- muliplcation
- power
- division
- model(取模?单词好像不对?)
these 6 kinds of operation.
So without further ado,let's shed light on what kinds of operation python supports?
4.1 arithmetic operators
4.1.1 addition
Example:
#produce 2
print(1+1)
4.1.2 subtraction
Example:
#produce 1
print(2-1)
4.1.3 multiplication
Example:
#produce 4
print(2*2)
4.1.4 power
Example:
#produce 9
print(3**2)
4.1.5 division and round down
Example:
#produce 0.3333333333333333(but i guess that the different CPU produce different outcomes)
print(1/3)
#produce 0.5
print(1/2)
#round down ->produce 1
print(3/2)
4.1.6 modulo operation
Example:
#produces 1
# 7 / 3 = 2...1
print(7%3)
4.2 comparsion operators
The outcome of comparsion operators is always bool.
operators | explanation |
---|---|
== | 3==1 ==> False ; 1==1= => True; |
> | 1>0 ==> True ; 0>1 ==> False; |
< | 1<0 ==>False ; 0<1 ==> True; |
>= | 1>=1 ==> True ; 0>=1 ==> False; |
<= | 1<=1 ==> True ; 2<=1 ==> False; |
4.3 assignment operators
operators | explanation |
---|---|
= | a=1 : assign 1 to a |
+= | a+=1 : a=a+1 |
-+ | a-=1 : a=a-1 |
*= | a=1 : a=a1 |
/= | a/=1 : a=a/1 |
%/ | a%=1 : a=a%1 |
**= | a=1;a=a1 |
:= | assign number to variable in expression |
4.4 logic operators
operators | explanation |
---|---|
and | equivalent to && |
or | equivalent to || |
not | equivalent to ! |
4.5 Identity operators
Note: Not simply equal,but if these two object are the same object ,literally with the same memory location.
operators | explanation |
---|---|
is | return true if same object |
is not | return true if different object |
4.6 Membership operators
Notes: usually use in list,tuple,set,dictionary
operators | explanation |
---|---|
in | return true if a sequence has this element |
not in | return true if a sequence doesn't have this element |
4.7 Bitewise operators????
5 Sequence
5.1 Introduction to sequence
- sequence is the most basic data structure in python.
- every element in sequence has its own index,begin with 0 and increases by order.
- There are six types of in-built sequences in python.
5.2 List
5.2.1 Definition
List is similar with array in Java,but it accepts several type in one list.
#declaration
a=[0,1,2,3]
5.2.2 index
the index starts from 0 and inceases by order.
Note:
There is another specific index system here:
the index start from the end of list and steps backwards decreasingly
5.2.3 seize List from a novel perspective : CRUD
1.Create
#declaration
a=[0,1,2,3]
2.Read
Index plays an important role in retrieving element here.
Literally we retrieves element with index to get element.
a[0] # a[0] = 0
3.Update
Genre | Explanation |
---|---|
change the original element | a[0] = 1 : change the element placed at index 0 to 1 |
append single element to the end of a list | a.append('a') a.append("hello") |
append another list to the end of a list | [0,1,2,3]+[4,5,6,7] |
duplicate list n times as a new list | [0,1]*2 : [0,1,0,1] |
slice(as dafny does) | shows below separately |
slice
[a:b] means cut from a to b but exclusive of b
a=[0,1,2,3,4,5]
print(a[:]) # [0,1,2,3,4,5]
print(a[0:]) # [0,1,2,3,4,5]
print(a[:1]) # [0]
print(a[0:1]) # [0]
4.delete
del() and listIdentifier.remove() both works
del a[5] # delete the element positioned at index 5
a.remove("hello") # remove first one matched element from the beginning
5.2.4 Nested list
a=[
[2,2],
[3,3],
[4,4],
1
]
print a
#outcome is : [[2, 2], [3, 3], [4, 4], 'hello']
5.2.5 compare different lists
import operator
a=[11]
b=[11]
c=[22]
print(operator.eq(a,b))
print(operator.eq(a,c))
print(operator.eq(a,b))
# True False
5.2.6 several crucial relevant functions
manipulate the list
function | description |
---|---|
len(list) | return the length of a list |
list(a) | a -> tuple or string : change a to a list |
min(a) | return the minimum in a list |
max(a) | return the maximum in a list |
list.reverse() | reverse a list |
list.clear() | clear a list out |
list.copy() | duplicate a list |
manipulate the element in a list
function | description |
---|---|
list.append(element) | element->any type : append to a list |
list.count(element) | count how many times this element occurs |
list.expand(seq) | seq->any kind of seq whatever tuper,list,set,dict : append this seq to the end of a list but if seq is dict then all keys are appended to the end |
list.index(obj) | return the index at which element matches obj |
list.insert(index,obj) | insert obj onto the position of list |
list.pop(index(:=-1)) | index->deault = -1 : pop up the last element |