Python
Dictionary
Written by Nichola Wilkin
1 © Nichola Wilkin 2013
Table of Contents
Python Basics .............................................................................................................. 5
Output ...................................................................................................................... 5
Line breaks............................................................................................................... 5
Combining outputs ................................................................................................... 5
Variables .................................................................................................................. 5
Inputting strings (text) ............................................................................................... 5
Inputting numbers ..................................................................................................... 5
Changing the Case of Text .......................................................................................... 6
Change to upper case .............................................................................................. 6
Change to lower case ............................................................................................... 6
Change to capitalise the first letter ........................................................................... 6
Change to capitalise each word ................................................................................ 6
Loops (Iteration) ........................................................................................................... 7
While loop ................................................................................................................ 7
For loop .................................................................................................................... 7
For loop with alternative range counter ..................................................................... 7
Comparison Operators ................................................................................................. 7
If Statements................................................................................................................ 8
Basic if statement ..................................................................................................... 8
Complex if statement ................................................................................................ 8
Maths in Python ........................................................................................................... 9
Addition .................................................................................................................... 9
Subtraction ............................................................................................................... 9
Multiplication ............................................................................................................ 9
Division .................................................................................................................... 9
Whole number division ............................................................................................. 9
Finding the remainder .............................................................................................. 9
Using Pi ( ) ............................................................................................................. 9
Random Library ......................................................................................................... 10
Importing the random library ................................................................................... 10
Generate a random whole number ......................................................................... 10
© Nichola Wilkin 2013 2
Generate a random decimal number ...................................................................... 10
Generate a random even number ........................................................................... 10
Generate a random choice from string list .............................................................. 10
Shuffle a list ............................................................................................................ 10
Time Library ............................................................................................................... 11
Import the time library ............................................................................................. 11
Show current time (unformatted) ............................................................................ 11
Show current time (formatted) ................................................................................ 11
Show current date (formatted) ................................................................................ 11
Creating a pause in the running of the program ...................................................... 11
Calculating with time .............................................................................................. 11
Calendar Library ........................................................................................................ 12
Import the calendar library ...................................................................................... 12
Print the calendar for the whole year ...................................................................... 12
Print the calendar for a single month ...................................................................... 12
Functions ................................................................................................................... 13
Creating a basic function ........................................................................................ 13
Running a function with an argument ..................................................................... 13
Running a function without an argument ................................................................ 13
Linking functions together ....................................................................................... 13
Lists ........................................................................................................................... 14
Creating a list ......................................................................................................... 14
Display the whole list .............................................................................................. 14
Display a single item from the list ........................................................................... 14
Adding to the end of the list .................................................................................... 14
Remove an item from a list ..................................................................................... 14
Inserting an item into a list ...................................................................................... 14
Reading and Writing to a Text File ............................................................................. 15
Appending data to the end of a text file .................................................................. 15
Writing several lines to a text file ............................................................................ 15
Displaying data (on screen) from a text file ............................................................. 15
Reading and Writing Data to a .csv File ..................................................................... 16
Importing the .csv library ........................................................................................ 16
3 © Nichola Wilkin 2013
Reading from a .csv file .......................................................................................... 16
Writing to a .csv file ................................................................................................ 16
Searching for data in a .csv file............................................................................... 16
© Nichola Wilkin 2013 4
Python Basics
Output
Line breaks
Combining outputs
Variables
Inputting strings (text)
Inputting numbers
5 © Nichola Wilkin 2013
Changing the Case of Text
Change to upper case
Change to lower case
Change to capitalise the first letter
Change to capitalise each word
© Nichola Wilkin 2013 6
Loops (Iteration)
While loop
For loop
For loop with alternative range counter
Comparison Operators
7 © Nichola Wilkin 2013
If Statements
Basic if statement
Complex if statement
© Nichola Wilkin 2013 8
Maths in Python
Addition
Subtraction
Multiplication
Division
Whole number division
Finding the remainder
Using Pi ( )
9 © Nichola Wilkin 2013
Random Library
Importing the random library
Generate a random whole number
Generate a random decimal number
Generate a random even number
Generate a random choice from string list
Shuffle a list
© Nichola Wilkin 2013 10
Time Library
Import the time library
Show current time (unformatted)
Show current time (formatted)
Show current date (formatted)
Creating a pause in the running of the program
Calculating with time
11 © Nichola Wilkin 2013
Calendar Library
Import the calendar library
Print the calendar for the whole year
Print the calendar for a single month
© Nichola Wilkin 2013 12
Functions
Creating a basic function
Running a function with an argument
Running a function without an argument
Linking functions together
13 © Nichola Wilkin 2013
Lists
Creating a list
Display the whole list
Display a single item from the list
Adding to the end of the list
Remove an item from a list
Inserting an item into a list
© Nichola Wilkin 2013 14
Reading and Writing to a Text File
Appending data to the end of a text file
Writing several lines to a text file
Displaying data (on screen) from a text file
15 © Nichola Wilkin 2013
Reading and Writing Data to a .csv File
Importing the .csv library
Reading from a .csv file
Writing to a .csv file
Searching for data in a .csv file
© Nichola Wilkin 2013 16