Python Base One,pythonbaseone
//this is my first day to study python, in order to review, every day i will make notes (2016/7/31)
1. In python , there are many bulit-in funcation. you can use follow to find out hou many built-in funcation:
dir(__builtins__)
if you want get BIF detail, you can use follow code:
help(……) such as help(input),int()
2. In python, you should be care to indent, if not , it will appear a lots error.
such as follow ,because indent error ,it will appear an error:
temp = input('input a num you want:')
guess = int(temp)
if guess == 8:
print('you are right')
else:
print('you are wrong')
3. In python , when you define variable ,uppercase and lowercase is different.
such as:
temp = 'roy'
Temp = 'wyg'
temp and Temp is not same variable
4. In python , if you want to use ' in a string ,you can user escape charcters : \
such as:
print('Let\'s go!')
5. In python , origin string is userful , maybe the follow result is not you except
such as :
str = 'c:\now\data'
print(str)
result is :
c:
ow\data
you can solve this problem by str = r'c:\now\data' ,this code is equal to str = 'c:\\now\\data'
when you print(str), the result will be c:\now\data
6. In python , if a string , you want to change row ,you can user ''' ,if you not user ''' and change row ,it will appear an error
such as:
str = '''this
is
me'''
print(str)
the result is :
this
is
me
7. In python , import module , such as if you want a rand num , range is 1-10 and type is int ,how to achieve it
import random
randnum = random.randint(1,10)
//this is second day to study python(2016/8/1)
8. In python , we can see 'e' as 10
such as:
1.5e4 == 15000.0
9. In python , type conversion is userful , the follow is base conversion
float or string -> int
such as :
a = '26' b = int(a) print(b) -> 26
a = 'qq' b = int(a) error
a = 3.14 b = int(a) print(b) -> 3
----------------------------------------
int or string -> float
such as:
a = '26' b = float(b) print(b) -> 26.0
a = 'qq' b = float(b) error
a = 26 b = float(b) print(b) -> 26.0
------------------------------------------
int or float -> str
such as:
a = 26 b = str(a) print(b) -> '26'
a = 3.14 b = str(a) print(b) -> '3.14'
a = 5e19 b = str(a) print(b) -> '5e+19'
sometimes, we need to be care to str,
such as:
str = 'I like apple'
print(str) -> 'I like apple'
but if it is c = str(5e19) ,it will appear an error ,because str is BIF ,we define str again , it will have new means, so it will have error
10. In python ,we can get the type of variable
such as:
a = 'qq'
type(a) -> <class 'str' at 0x----->
a = 3.0
type(a) -> <class 'float' at 0x--->
a = True
type(a) -> <class 'bool' at 0x--->
we recommand follow:
a = 'roy'
isinstance(a, str) -> True
isinstance(a, int) -> False
but sometimes you need to know it maybe account an error:
TypeError: isinstance() arg 2 must be a type or tuple of types
the reason is may you define str before, In python ,you can change built-in funcation, so when you define variable ,you should try to avoid user special chararctes. such as try not to use BIF.
11. In python , arithmetic operators has + - * / ** // %
a += 4 <==> a = a + 4
a = 2
b = 3
a / b -> 0.6666666666666666
a // b -> 0
b // a -> 1
a % b -> 2
b % a -> 1
a * b -> 8
b ** a -> 9
12. In python , logical operators includes and , or , not
such as:
not Ture
False
13. In python , if and else how to use:
score = int(input('please input score:'))
if 90<= score <= 100:
print('A')
elif 80<= score < 90:
print('B')
elif 60<= score < 80:
print('C')
else:
print('D')
14. trinocular operator
small = x if x < y else y
->
x, y = 4, 5
if x < y:
small = x
else:
small = y
15. assert ,when condication is false , it will have assertionerror
such as:
assert 3 > 4
>>> assert 3>4
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
assert 3>4
AssertionError
//third day study python (2016/8/2)
16. In python , how to achieve 'for' loop:
for target in expression:
loop body
such as:
string = 'roy'
for i in string:
print(i)
->
r
o
y
students = ['wyg' , 'roy]
for each in students:
print(each,len(each))
17. In python , range() is often combined with for loop , what's range()
range([start,] stop [, step = 1]) // three para
[] represent select
step = 1 represent by default step is 1
so it is from start to stop number list
range(3) -> range(0,3)
list(range(4)) -> [0, 1, 2, 3]
for i in range(2,5):
print(i)
result is :2 3 4
for i in range(1,10,4):
print(i)
result is :1 5 9
18. In python , there are also have break and continue
continue skip current loop
break end loop
19. In python , there are no array ,but have list and more power
such as:
mixType = [1, 'roy', 3.14, [5, 6]]
we can also create an empty list:
empty = []
how to add content to list:
empty.appen(3) -> 3
or
empty.extend([3, 6, 9]) -> 3 3 6 9
or
empty.insert(0, 'aaa') -> 3 'aaa 3 6 9