On Github YusiFan / pythonpresentation
Create by Yusi Fan @Cisco
#!/usr/bin/python # hello.py # import modules used here -- sys is a very standard one import sys # Gather our code in a main() function def main(): print 'Hello there', sys.argv[1] # Command line args are in sys.argv[1], sys.argv[2] ... # sys.argv[0] is the script name itself and can be ignored # Standard boilerplate to call the main() function to begin # the program. if __name__ == '__main__': main()
def check(speed,mood): if speed >= 80: print 'License and registration please' if mood == 'terrible' or speed >= 100: print 'You have the right to remain silent.' elif mood == 'bad' or speed >= 90: print "I'm going to have to write you a ticket." write_ticket() else: print "Let's try to keep it under 80 ok?"
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
Hello World! H llo llo World! Hello World!Hello World! Hello World!TESTStrings in Python are identified between quotation marks. Python allows for either pairs of single or double quotes. we can extract the Subsets of strings using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string. The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. For example: string method: s.lower(), s.upper(), s.strip(). The len(string) function returns the length of a string. The [ ] syntax and the len() function actually work on any sequence type -- strings, lists, etc.. Python tries to make its operations work consistently across different types.
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
['abcd', 786, 2.23, 'john', 70.2] abcd [786, 2.23] [2.23, 'john', 70.200000000000003] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.2, 123, 'john']Lists are the most commonly used data types in Python. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, items belonging to a list can be of different data type. The values stored in a list can be accessed using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator. For example: List method examples:list.append(elem), list.sort(), list.reverse(), list.index(elem)
nums = [1, 2, 3, 4] squares = [n * n for n in nums] #The syntax is [ expr for var in list ], result is [1, 4, 9, 16]
## Select fruits containing 'a', change to upper case fruits = ['apple', 'cherry', 'bannana', 'lemon'] afruits = [ s.upper() for s in fruits if 'a' in s ] ## ['APPLE', 'BANNANA']List comprehensions are a more advanced feature which is nice for some cases but is not needed for the exercises and is not something you need to learn at first (i.e. you can skip this section). A list comprehension is a compact way to write an expression that expands to a whole list. Suppose we have a list nums [1, 2, 3], here is the list comprehension to compute a list of their squares [1, 4, 9]: The syntax is [ expr for var in list ] -- the for var in list looks like a regular for-loop, but without the colon (:). The expr to its left is evaluated once for each element to give the values for the new list. Here is an example with strings, where each string is changed to upper case with '!!!' appended: You can add an if test to the right of the for-loop to narrow the result. The if test is evaluated for each element, including only the elements where the test is true.
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
('abcd', 786, 2.23, 'john', 70.200000000000003) abcd (786, 2.23) (2.23, 'john', 70.200000000000003) (123, 'john', 123, 'john') ('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists. For example:
Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is possible with lists:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 # Valid syntax with list
dict = {} dict['one'] = "This is one" dict[2] = "This is two" 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
This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object. Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [] ). For example: Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order"; they are simply unordered. Python's efficient key/value hash table structure is called a "dict". The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}. Looking up or setting a value in a dict uses square brackets, e.g. dict['foo'] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use "in" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case).
# print the numbers from 0 through 9 for i in range(10): ## equavilent to "for i in [0,1,2,3,4,5,6,7,8,9]" print iThe range(n) function yields the numbers 0, 1, ... n-1, and range(a, b) returns a, a+1, ... b-1 -- up to but not including the last number. The combination of the for-loop and the range() function allow you to build a traditional numeric for loop:
# Function definition is here 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]]All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example: Here, we are maintaining reference of the passed object and appending values in the same object. So, this would produce the following result:
# Function definition is here def printinfo( name, age = 35 ): # This prints a passed info into this function print 'Name: ', name print 'Age ', age return # Now you can call printinfo function printinfo( age=50, name='miki' ) printinfo( name='miki' )
Name: miki Age 50 Name: miki Age 35A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. Following example gives an idea on default arguments, it would print default age if it is not passed:
# Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them total = arg1 + arg2 print "Inside the function : ", total return total # Now you can call sum function total = sum( 10, 20 ) print "Outside the function : ", total
Inside the function : 30 Outside the function : 30The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None. All the above examples are not returning any value, but if you like you can return a value from a function as follows:
cat(filename)
# Echo the contents of a file def cat(filename): f = open(filename, 'r') for line in f: # iterates over the lines of the file print line, # trailing , so print does not add an end-of-line char # since 'line' already includes the end-of line. f.close()The next thing I want to show is File I/O. In the unix, there is a utility cat. and what it does is just print out the content of file. so i am just go ahead and re-create cat in python just to show you how to open files. i am gonna write a function call cat, it takes filename argument, what i want to do is just print the content of the file out. so the way to do this, there is built-in call open, it's first argument is a filename, the second argument is the mode, we will use "r", means we want to open the file for reading. the simplest way is to write a for loop over a file like it was a list. So i can say for line in f, what it does is read the file line by line and proecess it one line at a time. pirnt line i will fix up my main to call this thing, so cat will take the first command line argument print the content out. but i am getting this line doubling. the problem is this line it includes new line in the end. and when i print it, it put another new line. so to fix this, put a trailing command at the end that inhit the last newline, let's save that and run again, oh good. ok, that is one way to open it. using for loop. the advantage of this method is you can read a 47 GB of text file, this for loop will not require 47 GB of ram. it only use a tiny bit of ram, cause it deal with each line indepedently. if you can go line by line, this is an efficient way to do it.
readlines(): read file into a list, each element of list is a line in the file.
def cat(filename): f = open(filename,'r') list_of_lines = f.readlines() print list_of_lines f.close()The second way to do this is, there is function call readlines in python. what that does is, it read the entire file into a memory as a python list of line. each element of list is one line from the file. so if you have file that you want to process as a list, this is the way to do it. notice if the file is 28 GB of data, it require at least 28GB of ram to store
write(str): writes a string str to the file.
def write_file(filename,str): f = open(filename,'w') f.write(str) f.close()For writing files , use the built-in write, it takes a str argument that you want to write to the file.
import re str = 'an example word:cat!!' match = re.search(r'word:\w\w\w', str) # If-statement after search() tests if it succeeded if match: print 'found', match.group() ## 'found word:cat' else: print 'did not find'The regular expression in python are supported by a module call re. Let me fire up the intepreter here. I am gonna do some examples here. The basic idea of regular expression is the way of searching for a pattern inside a large of text. the simplest way, there is a function inside re called search, where the first argument of search is pattern, the second is the text. it returns a match object. import re, match = re.search(r'word:\w\w\w', str) The second methond I want to show is findall. what findall does is it find all the matched in the text and return to you as a list The 'r' at the start of the pattern string designates a python "raw" string which passes through backslashes without change which is very handy for regular expressions (Java needs this feature badly!). I recommend that you always write pattern strings with the 'r' just as a habit.
import re # Suppose we have a text with many email addresses str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher' # Here re.findall() returns a list of all the found email strings # ['alice@google.com', 'bob@abc.com'] emails = re.findall(r'[\w\.-]+@[\w\.-]+', str) for email in emails: # do something with each found email string print emailSuppose you want to find the email address inside the string 'xyz alice-b@google.com purple monkey'. We'll use this as a running example to demonstrate more regular expression features. Here's an attempt using the pattern r'\w+@\w+': The search does not get the whole email address in this case because the \w does not match the '-' or '.' in the address. We'll fix this using the regular expression features below. Square brackets can be used to indicate a set of chars, so [abc] matches 'a' or 'b' or 'c'. The codes \w, \s etc. work inside square brackets too with the one exception that dot (.) just means a literal dot. For the emails problem, the square brackets are an easy way to add '.' and '-' to the set of chars which can appear around the @ with the pattern r'[\w.-]+@[\w.-]+' to get the whole email address: (More square-bracket features) You can also use a dash to indicate a range, so [a-z] matches all lowercase letters. To use a dash without indicating a range, put the dash last, e.g. [abc-]. An up-hat (^) at the start of a square-bracket set inverts it, so [^ab] means any char except 'a' or 'b'. findall() is probably the single most powerful function in the re module. Above we used re.search() to find the first match for a pattern. findall() finds *all* the matches and returns them as a list of strings, with each string representing one match. You can use the sub method to do substitution. (link here)
os.listdir(path)
os.get cwd()
os.mkdir(dir)
The module i want to talk about is the os module. it stands for operating system i think. so i am gonna do dir for it. it give a list of functions inside this modules. you can see the method like getpid which get the process id, kill to kill the process. I would like to show is listdir. help(os.listdir)