COQUITLAM
COLLEGE
Established 1982
Python Programming 101
What You Will Learn
• What is Python IDLE?
– What is the difference between interactive and script mode
• Python
– Comments
– Statements
– Output
– Mathematical expressions
– String operations
• Where to find help on Python?
Coquitlam College CSCI120 2
Python IDLE
• Python’s Integrated Development and Learning Environment.
• Interactive mode in the shell window
– interactive interpreter
– >>> (type the code, and press enter)
– result is displayed.
• Script mode in the editor window
– write code (a script) and save as a module
– name for a module has extension .py
– run module
– result is displayed in the shell window
• Debugger
Coquitlam College CSCI120 3
Python
Syntax, Keywords
• Syntax
– High-level languages have strict rules how to built structurally correct
programs.
– Syntax rules
• Syntax Errors
– not obeying the syntax rules
• Keywords in Python
– reserved words with a specific meaning in Python
– cannot be used as variable names, function names, or any other
identifiers
– here is the complete list of keywords
Coquitlam College CSCI120 4
Comments in Python
• Example
# Demo output in Python
# Author: Rita Ester
# Date: 21-08-2019
• Line starts with #
• Why comments?
– for documentation of a program
– give explanations
Coquitlam College CSCI120 5
Output
• The print function is used for output
– Example: print("Hello World")
• Text output
– print("Hello World")
– displays: Hello World
• Number output
– print (9.5)
– displays: 9.5
• Mathematical expressions
– print (3+4)
– displays: 7
Coquitlam College CSCI120 6
Mathematical Expressions
• Numeric expressions
• Python evaluates an expression.
• Examples of mathematical expressions
– 3+4
– 3*4
– 21 + 3*7
– (3+5) * (2/0.5)
• Structure of an expression 3576.78 + 4000
Operand Operator Operand
Coquitlam College CSCI120 7
Mathematical Operators
Operation Symbol in Python Example
Power (exponentiation) ** 5**2 == 25
Multiplication * 2*3 == 6
Division / 14 / 3 ==
4.666666666666
Integer Division // 14 // 3 == 4
(floor division)
Remainder (modulus) % 14 % 3 == 2
Addition + 1 + 2 == 3
Subtraction - 4 - 3 == 1
Coquitlam College CSCI120 8
Mathematical Expressions
• How to evaluate mathematical expressions?
– Precedence rules – what to evaluate first?
1. Parentheses ()
2. Exponentiation: **
3. Multiplication, division, floor division, and remainder: * / // %
4. Addition and subtraction: + −
5. left to right
Coquitlam College CSCI120 9
Strings and String Operations
• What is a string?
– the way to represent plain text.
– "Hello World" enclosed in double quotation marks
– 'Hello World' enclosed in single quotation marks
• String concatenation
– "Hello " + "World"
– print("Hello " + "World"), displays: Hello World
• String multiplication
– 3 * "Hello "
– print(3*"Hello "), displays: Hello Hello Hello
Coquitlam College CSCI120 10
Escape Sequences
• Combination of characters with a special effect for printing.
• \n creates a new line.
• \t creates a tabulator space
• \\ prints one \
• Example
– this will print:
Coquitlam College CSCI120 11