python’101 · python’code output Docs names’='Jon’Roberts\tErnie’Mross'...

Preview:

Citation preview

python  101

Docs

ge/ng  python

h0p://www.python.org

Docs

IDLE  -­‐  the  python  development  environment

python  code

output

Docs

print()print('Hello  World!')print('UlGmate  answer:  ',  27  +  15)

Hello  World!UlGmate  answer:    42

Hello  World!

<-­‐  blank  line

python  code

output

Docs

string

x  =  'abcde'print(x)

abcde

python  code

output

Docs

i  =  2j  =  3print('integer  divide  2/3:  ',  i/j)

integer  divide  2/3:    0

integer

python  code

output

Docs

i  =  2.0    #floatj  =  3          #  integerprint('floaGng  point  divide  2.0/3:  ',  i/j)

floaGng  point  divide  2.0/3:    0.666666666667

float

python  code

output

Docs

x  =  'abcde'print(x[0])print(x[-­‐1])print(x[1:4])

aebcd

sequence  -­‐  string

python  starts  counGng  at  zero

python  code

output

Docs

x  =  (1,2,3,4,5)print(x[0])print(x[-­‐1])print(x[1:4])

15(2,  3,  4)

sequence  -­‐  tuple

python  code

output

Docs

x  =  [1,2,3,4,5]print(x[0])print(x[-­‐1])print(x[1:4])

15[2,  3,  4]

sequence  -­‐  list

python  code

output

Docs

x  =  (1,2,3,4,5)x[0]  =  9

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

tuple  is  immutable

python  code

output

Docs

x  =  [1,2,3,4,5]x[0]  =  9print(x)

[9,  2,  3,  4,  5]

list  is  mutable

python  code

output

Docs

x  =  [1,2,3,4,5]x[0]  =  'a'x[1]  =  (4,5,6)print(x)

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

lists  can  have  mixed  types

python  code

output

Docs

answer  =  42pi  =  3.141592654print('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))

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

string  %  forma/ng

tab

line  break

python  code

output

Docs

print(float(42))print(int(5.75))print(round(5.75))print(int(round(5.75)))print(range(5))

42.056.06[0,  1,  2,  3,  4]

funcGons()

python  code

output

Docs

import  randomnumbers  =  [1,2,3,4,5]random.shuffle(numbers)print(numbers)

[2,  5,  4,  1,  3]

funcGons  from  imported  libraries

python  code

output

Docs

names  =  'Jon  Roberts\tErnie  Mross'print(1,  names.split())print(2,  names.split('\t'))print(3,  names.split('\t')[0])print(4,  names.lower().split('\t')[0].split())

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

methods  -­‐  funcGons  “a0ached”  to  objects

'\t'  is  the  tab  character

python  code

output

Docs

words  =  ['one','two','three']if  'three'  in  words:        print('found  three')

found  three

what  if?

python  code

output

Docs

x  =  5if  x  >5:        print('big  x')else:        print('x  is  less  than  or  equal  to  5')

x  is  less  than  or  equal  to  5

more  if?

python  code

output

Docs

x  =  1if  x  ==  3:  

print('three')elif  x  ==  5:

print('five')else:

print('not  found')

not  found

even  more  if?

python  code

output

Docs

for  num  in  range(4):        x  =  num+10        print(x)print('done')

10111213done

loops

python  code

output

Docs

nameList  =  ['jon','ernie','linda']for  aName  in  nameList:        print('aName  -­‐>  ',  aName)

aName  -­‐>    jonaName  -­‐>    ernieaName  -­‐>    linda

more  loops

python  code

output

Docs

done  =  False  ;  x  =  0while  not  done:        x  +=  3        if  x>9:  done  =  True        print(x)

36912

even  more  loops

python  code

output

Docs

infile  =  open('tabtext.txt',  mode='rU')for  aLine  in  infile:        print(aLine.split('\t'))infile.close()

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

reading  files

python  code

output

Docs

infile  =  open('tabtext.txt',  mode='rU')for  aLine  in  infile:        print(aLine.strip().split('\t'))infile.close()

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

more  reading  files

python  code

output

Docs

data  =  [[1,2],[3,4]]ouoile  =  open('tabtextout.txt',  mode='w')for  subList  in  data:        ouoile.write('%i\t%i\n'  %  (subList[0],subList[1]))ouoile.close()

1      23      4

wriGng  files

‘w’  for  write,  ‘a’  for  append

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

tabs

line  breaks

Recommended