Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import division
- a=7/3
- b=round(a, 4)
- c=9
- d=10
- c+=1
- d-=1
- print (a)
- print(b)
- print(c)
- print(d)
- new_file=open("file.txt")
- # default mode is a read
- open_new_file=new_file.read()
- print (open_new_file)
- #real world application
- new_file.close()
- #reading file line by line
- f_readline=open("file.txt")
- open_f_readline=f_readline.readline()
- print(open_f_readline)
- #reading all the lines in one goal: it will return a list that have all the lines of the file
- f_readlines=open("file.txt")
- open_f_readlines=f_readlines.readlines()
- print(open_f_readlines)
- #contest management way, python automatically close the file handle.
- with open("file.txt") as f:
- output=f.readline()
- print (output)
- #write operations
- v=open('new_file1.txt', mode='w')
- v.write('something special\n')
- v.flush()
- #write operation
- with open('new_file.txt', mode='w') as m:
- m_output=m.write('something special\n')
- #ppend operation
- z=open('new_file1.txt', mode='a')
- z.write('something special\n')
- z.flush()
- ########################################
- ############LISTS######################
- #######################################
- my_list=[]
- my_list.append('tosin')
- print (my_list)
- my_list.append('dayo')
- print( (my_list) + [1,2,3])
- my_new_list=[1,2,3,'tosin','dayo']
- x=my_new_list.extend([10,'dayo'])
- print(x)
- #remove the last element
- y=x.pop()
- #removes the last on the list
- print(y)
- #remove from the first index
- my_new_list.pop(0)
- #OR
- del my_new_list[2]
- #counts number of times 'tosin' is in the list
- my_list.count('tosin')
- #index where hello first appeared
- my_list.index('tosin')
- # https://www.tutorialspoint.com/python/python_lists.htm
- my_list=[1,2,3,'amazon','google']
- new_list=['UN','hertz']
- my_list.append(new_list)
- for i in range(0,len(my_list)):
- print i
- for i in range(0,len(my_list)):
- print (str(i) +' is equal to : ' + str(my_list[i]))
- ##########TUPLE############
- txt = 'but soft what light in yonder window breaks'
- words = txt.split()
- t = list()
- for word in words:
- t.append((len(word), word))
- t.sort(reverse=True)
- res = list()
- for length, word in t:
- res.append(word)
- print res
- d = {'a':10, 'b':1, 'c':22}
- l = list()
- for key, val in d.items() :
- l.append( (val, key) )
- datacenter1 = {'spines': ['r1', 'r2', 'r3', 'r4']}
- datacenter1['leafs']=['l1', 'l2', 'l3', 'l4']
- for i,j in datacenter1.items():
- print(i,j)
- l
- m = [ 'have', 'fun' ]
- x, y = m
- x
- y
- d = {'a':10, 'b':1, 'c':22}
- t=d.items()
- print t
- ipv6='2001:0db8:85a3:0000:0000:8a2e:0370:7334'
- x=ipv6.split(':')
- y=(':').join(x)
- ******SYS>ARGV*****
- #!/usr/bin/enc python
- from __future__ import print_function
- import sys
- print(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement