Skip to content

Instantly share code, notes, and snippets.

@eellpp
Last active March 28, 2016 12:14
Show Gist options
  • Save eellpp/2f34e3fd5014e3b82dd1 to your computer and use it in GitHub Desktop.
Save eellpp/2f34e3fd5014e3b82dd1 to your computer and use it in GitHub Desktop.
Reference Cards
http://www.astro.up.pt/~sousasag/Python_For_Astronomers/Python_qr.pdf
Regular expression
https://developers.google.com/edu/python/regular-expressions?hl=en
Python Basics
Python has five standard data types −
- Numbers
- String
- List
- Tuple
- Dictionary
### Strings
str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
### List
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
del list1[2];
len([1, 2, 3])
[1, 2, 3] + [4, 5, 6]
3 in [1, 2, 3]
for x in [1, 2, 3]: print x
### Tuple
They are readonly
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints complete list
print tuple[0] # Prints first element of the list
print tuple[1:3] # Prints elements starting from 2nd till 3rd
print tuple[2:] # Prints elements starting from 3rd element
print tinytuple * 2 # Prints list two times
print tuple + tinytuple # Prints concatenated lists
len((1, 2, 3))
(1, 2, 3) + (4, 5, 6)
3 in (1, 2, 3)
for x in (1, 2, 3): print x
### Dictionary
Python's dictionaries are kind of hash table type
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
del dict['one'];
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
len(dict)
seq = ('name', 'age', 'sex')
dict = dict.fromkeys(seq)
dict.has_key(key)
dict.items() # Returns a list of dict's (key, value) tuple pairs
dict.keys()
dict.values()
dict.update(dict2)
### Python Membership Operators
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.
x in y
x not in y
### Python Identity Operators
Identity operators compare the memory locations of two objects. Both should be poiting to same object
x is y
x is not y
str.find(str2, beg=0 end=len(string))
"-".join("a","b","c")
str.split(str=" ")
str.splitlines()
str.strip()
### Functions arguments are passed by reference
def changeme( mylist ):
"This changes a passed list into this function"
mylist.append([1,2,3,4]);
print "Values inside the function: ", mylist
return
# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
### Default value
def printinfo( name, age = 35 ):
Variable args
def printinfo( arg1, *vartuple ):
for var in vartuple:
print var
# Now you can call printinfo function
printinfo( 70, 60, 50 )
### Module import
import module1[, module2[,... moduleN]
from modname import name1[, name2[, ... nameN]]
# Now import your Phone Package.
import Phone
Phone.Pots()
Phone.Isdn()
Phone.G3()
input
str = input("Enter your input: ");
[x*5 for x in range(2,10,2)]
fo = open("foo.txt", "r")
str = fo.read(10);
fo.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment