0% found this document useful (0 votes)
6 views

Python

Uploaded by

Thanh Van Dao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python

Uploaded by

Thanh Van Dao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

python

 101

CuuDuongThanCong.com
ge/ng  python

h0p://www.python.org

Docs
CuuDuongThanCong.com
IDLE  -­‐  the  python  development  environment

Docs
CuuDuongThanCong.com
Hello  World!
python  code
print()
print('Hello  World!')
print('UlGmate  answer:  ',  27  +  15)

output
<-­‐  blank  line
Hello  World!
UlGmate  answer:    42

Docs
CuuDuongThanCong.com
string
python  code
x  =  'abcde'
print(x)

output
abcde

Docs
CuuDuongThanCong.com
integer
python  code
i  =  2
j  =  3
print('integer  divide  2/3:  ',  i/j)

output
integer  divide  2/3:    0

Docs
CuuDuongThanCong.com
float
python  code
i  =  2.0    #float
j  =  3          #  integer
print('floaGng  point  divide  2.0/3:  ',  i/j)

output
floaGng  point  divide  2.0/3:    0.666666666667

Docs
CuuDuongThanCong.com
sequence  -­‐  string
python  code
x  =  'abcde'
print(x[0]) python  starts  counGng  at  zero
print(x[-­‐1])
print(x[1:4])

output
a
e
bcd

Docs
CuuDuongThanCong.com
sequence  -­‐  tuple
python  code
x  =  (1,2,3,4,5)
print(x[0])
print(x[-­‐1])
print(x[1:4])

output
1
5
(2,  3,  4)

Docs
CuuDuongThanCong.com
sequence  -­‐  list
python  code
x  =  [1,2,3,4,5]
print(x[0])
print(x[-­‐1])
print(x[1:4])

output
1
5
[2,  3,  4]

Docs
CuuDuongThanCong.com
tuple  is  immutable
python  code
x  =  (1,2,3,4,5)
x[0]  =  9

output
Traceback  (most  recent  call  last):
   File  "/Users/jroberts/Desktop/test.py",  line  2,  in  <module>
       x[0]  =  9
TypeError:  'tuple'  object  does  not  support  item  assignment

Docs
CuuDuongThanCong.com
list  is  mutable
python  code
x  =  [1,2,3,4,5]
x[0]  =  9
print(x)

output
[9,  2,  3,  4,  5]

Docs
CuuDuongThanCong.com
lists  can  have  mixed  types
python  code
x  =  [1,2,3,4,5]
x[0]  =  'a'
x[1]  =  (4,5,6)
print(x)

output
['a',  (4,  5,  6),  3,  4,  5]

Docs
CuuDuongThanCong.com
string  %  forma/ng
python  code
answer  =  42
pi  =  3.141592654
print('the  answer  is  %i  and  pi  is  approx.  %f'  %  (answer,pi))
print('the  answer  is  %i  and  pi  is  approx.  %.2f'  %  (answer,pi))
print('%i\t%.2f\n'  %  (answer,pi))

output
the  answer  is  42  and  pi  is  approx.  3.141593
the  answer  is  42  and  pi  is  approx.  3.14
42   3.14

line  break

tab

Docs
CuuDuongThanCong.com
funcGons()
python  code
print(float(42))
print(int(5.75))
print(round(5.75))
print(int(round(5.75)))
print(range(5))

output
42.0
5
6.0
6
[0,  1,  2,  3,  4]

Docs
CuuDuongThanCong.com
funcGons  from  imported  libraries
python  code
import  random
numbers  =  [1,2,3,4,5]
random.shuffle(numbers)
print(numbers)

output
[2,  5,  4,  1,  3]

Docs
CuuDuongThanCong.com
methods  -­‐  funcGons  “a0ached”  to  objects
python  code
names  =  'Jon  Roberts\tErnie  Mross'
print(1,  names.split())
print(2,  names.split('\t')) '\t'  is  the  tab  character
print(3,  names.split('\t')[0])
print(4,  names.lower().split('\t')[0].split())

output
1  ['Jon',  'Roberts',  'Ernie',  'Mross']
2  ['Jon  Roberts',  'Ernie  Mross']
3  Jon  Roberts
4  ['jon',  'roberts']

Docs
CuuDuongThanCong.com
what  if?
python  code
words  =  ['one','two','three']
if  'three'  in  words:
       print('found  three')

output
found  three

Docs
CuuDuongThanCong.com
more  if?
python  code
x  =  5
if  x  >5:
       print('big  x')
else:
       print('x  is  less  than  or  equal  to  5')

output
x  is  less  than  or  equal  to  5

Docs
CuuDuongThanCong.com
even  more  if?
python  code
x  =  1
if  x  ==  3:  
print('three')
elif  x  ==  5:
print('five')
else:
print('not  found')

output
not  found

Docs
CuuDuongThanCong.com
loops
python  code
for  num  in  range(4):
       x  =  num+10
       print(x)
print('done')

output
10
11
12
13
done

Docs
CuuDuongThanCong.com
more  loops
python  code
nameList  =  ['jon','ernie','linda']
for  aName  in  nameList:
       print('aName  -­‐>  ',  aName)

output
aName  -­‐>    jon
aName  -­‐>    ernie
aName  -­‐>    linda

Docs
CuuDuongThanCong.com
even  more  loops
python  code
done  =  False  ;  x  =  0
while  not  done:
       x  +=  3
       if  x>9:  done  =  True
       print(x)

output
3
6
9
12

Docs
CuuDuongThanCong.com
reading  files
python  code
infile  =  open('tabtext.txt',  mode='rU')
for  aLine  in  infile:
       print(aLine.split('\t'))
infile.close()

output
['line  1',  'abc',  'def\n']
['line  2',  'ghi',  'jkl']

Docs
CuuDuongThanCong.com
more  reading  files
python  code
infile  =  open('tabtext.txt',  mode='rU')
for  aLine  in  infile:
       print(aLine.strip().split('\t'))
infile.close()

output
['line  1',  'abc',  'def']
['line  2',  'ghi',  'jkl']

Docs
CuuDuongThanCong.com
wriGng  files
python  code
data  =  [[1,2],[3,4]]
ouoile  =  open('tabtextout.txt',  mode='w') ‘w’  for  write,  ‘a’  for  append
for  subList  in  data:
       ouoile.write('%i\t%i\n'  %  (subList[0],subList[1]))
ouoile.close()

you  must  close  the  file  to  be  certain  all  data  are  wri0en

output
1      2
3      4

line  breaks

tabs

Docs
CuuDuongThanCong.com

You might also like