An introduction course to Python – Python Program – Standard Data Types



An introduction course to Python – Python Program – Standard Data Types

0 0


pythonpresentation

Python presentation using reveal.js

On Github YusiFan / pythonpresentation

An introduction course to Python

Create by Yusi Fan @Cisco

Overview

  • Background
  • Syntax
  • Types/Operators/Control Flow
  • Functions
  • File IO
  • Regular Expression
  • System call
  • Tools and resource

What is Python

  • Multi-purpose (Web, GUI, Scripting, etc)
  • Object Oriented
  • Interpreted
  • Strongly typed and Dynamically typed
  • Focus on readability and productivity
Multi-purpose Python is multipurpose: it is not specialised to a specific target of users. Python can be used for any programming task, from GUI programming to web programming with everything else in between.And python is widely used for scripting. It's quite efficient, as much of its activity is done at the C level. Python is just a layer on top of C. There are libraries for everything you can think of: game programming and openGL, web frameworks, semantic web, scientific computing... it also Comes with a large standard library that supports many common programming tasks such as connecting to web servers, searching text with regular expressions, reading and modifying files. Object oriented: Python is object oriented. Everything is an object (including classes, functions, modules, etc), in the sense that they can be passed around as arguments, have methods and attributes, and so on. Interpreted: There is no compilation process like java and c++. Python Code Is Checked At Runtime. Dynamically: Python is strongly typed and dynamically typed: You don't declare a type for a variable name, and then assign something of that type. Instead, you create variable and asssign value to it.  Stronly typed: Python is strongly typed. For example if you assign a string to a variable, and later you to convert the variable to type integer. you need to do it explicitly. Every type conversion in python must be done explicitly. This is different from, for example, Perl or Javascript. Readability and productivity: For example, Python force developers to indent code in the code block. There are no currly braces in Python. Blocks of code are identified by the level of indentation. It makes the code easy to read. You will see the identation is not optional in python code later.

Releases

  • Created in 1989 by Guido van Rossum
  • Python 1.0 - January 1994
  • Python 2.0 was released on 16 October 2000
  • Python 3.0, a major, backwards-incompatible release, was released on 3 December 2008
Short version: Python 2.x is legacy, Python 3.x is the present and future of the language /router/bin/python3-3.4.0 ls | grep 'python‘ Python was first released in 1989 by Guido van Rossum. Then in 1994, python 1.0 was released. python 2.0 was releasd in 2000. And then in 2008, Python 3.0 was released. Since it is backwards-incompatible, it took a long time to adopt. Until today, there are still some applications and third party modules using python 2.7, but as language itself python 3.0 is mature enough. you can use either 2.7 or 3 About the origin of Python, Van Rossum wrote in 1996: Over six years ago, in December 1989, I was looking for a "hobby" programming project that would keep me occupied during the week around Christmas. My office ... would be closed, but I had a home computer, and not much else on my hands. I decided to write an interpreter for the new scripting language I had been thinking about lately: a descendant of ABC that would appeal to Unix/C hackers. I chose Python as a working title for the project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying Circus)(Link).

Popularity From TIOBE

As a programming language, python is very popular. According to the TIOBE Programming Community index which is an indicator of the popularity of programming languages. Python is rated as number 7 among other languages. Perl is rated as number 9. They both have large amount of users, but later when you see the python code, the syntax is much cleaner and simpler.

When to choose Python over Perl?

  • The script is used more than once.
  • Your colleagues or someone else will ever use your script.
  • Whenever you need to use functions or objects
  • Anything you can do in Perl also in Python
Python has a very simple programming philosophy, "there should be one and preferably only one obvious way to do it". This is a characteristic that sets Python apart from its competitors such as Perl. It makes python code cleaner and easier to understand. Perl on the other hand sees programming as, "there is more than one way to do it". This is evident in Perl's regular expressions. Based on my personal experience and other users that have switched from perl to python. Here is the reasons that you should use python.

Who uses Python

and more...

Python is widely used. There are many companies that use python. Many components of the Google spider and search engine are written in Python. The YouTube is largely written in Python. Cisco: The open stack that the cloud team used are 100% pure Python. Yahoo, whose address and mapping lookup services are implemented in Python. Instagram whose website is built using python and the python web framework Django. Dropbox: etc.

Two Modes

  • Interactive Mode Programming
  • Script Mode Programming
There are two modes that you can run python code. The first one is the interactive mode. Let's open the terminal to see how to run code in interactive mode. Since python is already installed on our server, you simply type python on the command line. And we are in the interactive mode, this is the python intepreter, the version is 2.7.4. An excellent way to see how Python code works is type code right into it. If you ever have a question like "what happens if I add an integer to a list?" ... just typing it into the Python interpreter is fast way to see what happens. Let's try it out. (In interactive mode, try examples: fruits = ['apple','banana']; fruit; fruit.append('orange'); len(fruits); a = 6; a; a+2; a='Hi'; a; len(a); foo(a); Ctrl+D) The second mode is the script mode.It's the normal way we usually write code. You write the code an save as a python file, then you can reuse it later.

Python Program

#!/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()
	
  • hash sign (#) is used to start comment
  • colon (:) is used to start a block
  • indentation used to group block
(vi hellothere.py) Here's a very basic Python program appear at the very top, you have this standard usr/bin, it indicate what interpreter can run the script first we import sys module, module is a library that has functions you can use. in sys module,it has functions to access operating system interface, file system. in python you use import statement to refer to some external modules you want to pull in, then we define the main function, or whatever name you like. then colon, inside the main function ,to refer to something that inside the module you say the module name sys then the name, argv is the name of command line in that module the list sys.argv contains the command line arguments, sys.argv[1] the first argument, and so on. then if statement, it's a biolerplate syntax to run the main function. The way it works is that the hello.py, we can invoke it directly from the command line, in this case the if statment is true. then it calls the main function we use colon to start a block, and indentation to group block You can use any editor you want, i will use vim. First we add the shebang, when we run the script it knows where to find the python intepreter. then we import a python module called sys, a module is library. then we def a function called main, or you can name whatever you want. what this line means is if the this python script is run directly, the __name__ varialbe is set to main. With the statement "import sys" you can can then access the functions in the sys module sys -- access to exit(), argv, stdin, stdout, ... re -- regular expressions os -- operating system interface, file system You can find the documentation of all the Standard Library modules and packages at http://docs.python.org/library. There are many modules and packages which are bundled with a standard installation of the Python interpreter, so you don't have do anything extra to use them.

Function / Indentation

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?"
  • indentation is not optional
  • indentation is used to group block
  • A logical block of statements should all have the same indentation
Python uses the colon (:) and indentation/whitespace to group statements. Thus, in Python all the continous lines indented with similar number of spaces would form a block. The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. Both blocks in this example are fine: However, the second block in this example(different indentations in one block) will generate an error A logical block of statements such as the ones that make up a function should all have the same indentation, set in from the indentation of their parent function or "if" or whatever. If one of the lines in a group has a different indentation, it is flagged as a syntax error. Python's use of whitespace feels a little strange at first, but it's logical and I found I got used to it very quickly. Avoid using TABs as they greatly complicate the indentation scheme (not to mention TABs may mean different things on different platforms). Set your editor to insert spaces instead of TABs for Python code. A common question beginners ask is, "How many spaces should I indent?" According to the official Python style guidelines (PEP 8), you should indent with 4 spaces. (Fun fact: Google's internal style guideline dictates indenting by 2 spaces!)

Standard Data Types

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary
Python has five standard types that are used to define the operations possible on them and the storage method for each of them.

Numbers

String

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
		
This will produce the following result:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
    	
Strings 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

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
		
This will produce the following result:
['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)

List Comprehensions

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.

Tuples

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
		
This will produce the following result:
('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:

Tuples

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
    	

Dictionary

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 will produce the following result:
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).

Python Operators

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators:

Comparison Operators

Assignment Operators

Bitwise Operators

Logical Operators

Membership Operators

Identity Operators

Control Flow

  • If..elif..else
  • While loop
  • For loop

If..elif..else

While loop

For loop

For loop

Range

# 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 i
            	
The 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:
The for/in constructs are very commonly used in Python code and work on data types other than list. You may have habits from other languages where you start manually iterating over a collection, where in Python you should just use for/in. You can also use for/in to work on a string. The string acts like a list of its chars, so for ch in s: print ch prints all the chars in a string. An alternative way of iterating through each item is by index offset into the sequence itself. Following is a simple example: Here, we took the assistance of the len() built-in function, which provides the total number of elements in the tuple as well as the range() built-in function to give us the actual sequence to iterate over.

Function / Pass by reference vs value

# 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
	    	
This would produce the following result
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:

Default arguments

# 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' )
	    	
This would produce the following result
Name:  miki
Age  50
Name:  miki
Age  35
	    	
A 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:

The return Statement

# 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
	    	
This would produce the following result
Inside the function :  30
Outside the function :  30
	    	
The 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:

File I/O

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.

File IO

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

File IO

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.

Regular Expressions

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.

findall

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 email
				
Suppose 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)

system call (os module)

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)

Resource

THE END

BY Yusi Fan / @Cisco