Python Programming
Copyright © 2023 Olympiads School. All rights reserved
1
Variables
"If I have seen further it is by standing on the shoulders of Giants.“
Isaac Newton, 1675
“Give a Man a Fish, and You Feed Him for a Day. Teach a Man To Fish,
and You Feed Him for a Lifetime.”
Anonymous
2
• Documentation for the Python
• Python 3 documentation is available online at
https://docs.python.org/3/
• Go ahead: bookmark this link in your web browser
• Python Library:
https://docs.python.org/3/library/index.html
• Python built-in functions:
https://docs.python.org/3/library/functions.html
• Python Library math module:
https://docs.python.org/3/library/math.html
3
Python Module
• A Python module is a python file that defines variables and functions
• Your program may import other modules to access them. This allows for code reuse
• The Python library contains many useful modules, such as the math module:
4
Functions
• A function is a group of related statements that perform a specific task
• The math module contains many commonly used mathematical functions, such as the
power function xy and square root function
5
Constants
• Constants are values pre-defined in a module
6
Variables
• In Python, variables are automatically defined when they are assigned a value
• Variables can store these types of value:
• Numbers
• String
• Tuple
• List
• Set
• Dictionary
• Functions
(They will be introduced later in the course)
7
In class exercise #1 – Calculate the area of a circle with radius 2.4
• Create new Python file by New | Python File
• Type the following code:
8
• Run the program and see the output:
• Note these language features in the code:
• Import of a module
• Variable assignment
• Multiplication operator
• Use of math.pi constant
• Invocation of function
• Formatting of float number
9
Congratulations!
You have just written your
first Mathematical Program
10
Variable Names
• Can be any length
• Consist of uppercase / lowercase letters, digits and underscore
• Must not start with a digit
Mathematical operators
• +: add
• -: subtract
• *: multiply
• /: divide
• //: integer divide
• %: modulo
11
In class exercise #2: Calculate cosines
• Create a Python program to print the following values to 6 decimal places:
• cosine (0.1)
• cosine (0.2)
• cosine (0.3)
• cosine (π)
• Use the cos() function in the math module:
e.g.
12
In class exercise #3: Quadratic Formula
• This is a quadratic equation, with a, b, c being real numbers, and a not equal to zero:
• We can use the quadratic formula to calculate the two possible values of x:
• Print the two real solutions of the following quadratic equation:
13
Input value
The line of code above does the following:
1. Print a prompt “Please enter a string value: ”
2. Halt the program until the user provides a string input (until enter key is pressed)
3. Assign the string input value to variable “x”
The line of code above does the following:
1. Print a prompt “Please enter a float value: ”
2. Halt the program until the user provides a float number input
3. Assign the float input value to variable “y”
14
In class exercise #4: Input
• Update the solution from exercise 3 so that the program will prompt the user to provide the values
of a, b and c instead of hard-coding them in the program
15
Print formatting
The following string formatting are used
• %d in place of an integer value
• %s in place of a string value
• %.2f in place of a float value with 2 decimal places
16
String operator
• +: concatenate two strings
e.g. “Hello “ + “World” evaluates to “Hello World”
• upper() converts a string to uppercase
e.g. “Hello”.upper() evaluates to “HELLO”
• lower() converts a string to lowercase
e.g. “Hello”.lower() evaluates to “hello”
17
Formatting characters
• \n: new line
• \r: carriage return
• \t: tab
• \\: backslash
• \”: double quote
18
In class exercise #5: Output
• Print the first 10 prime numbers in a line, separated by the tab character (\t)
19
Performance Profiling
• Mathematical tasks may take a long time to run
• We can track the amount of time it takes to execute a program by
• Importing time module and calling time.time() to mark the start time of the program
• Calling time.time() again to mark the end time of the program
• Display the difference of the two timestamps
• We can use this technique to identify where a program is running slowly, or inefficiently
20
In class exercise #6: Performance Profiling
• Run the following code and determine how long it takes your computer to calculate cosine(0.2) one
million times
21
• How long did it take to run on your machine?
22