Lecture 2.1 After Large
Lecture 2.1 After Large
• Reading
• cspy ch. 1 & 2
• Compilers read the source code completely and translate it to machine code
that can be executed by computer.
• C, C++, Java, …
• Running script
• Save the program in a file using a text editor
• example.py
• Using interpreter to run the content of file
• > python example.py
• A program consists of
• Input, output, arithmetic and logical operations, conditional
execution and repetition
• Programming is breaking large and/or sophisticated
tasks to subtasks
• And continuing this process until the subtasks are simple
enough to be executed with basic instructions.
Hello World!
Strings in Python can be
>>> print("Hello, World!") enclosed in different ways.
>>> print("""H2O’’SO2+/9""")
1. H2OSO2+/9
2. ""H2O’’SO2+/9""
3. "H2O’’SO2+/9"
4. H2O’’SO2+/9
https://www.linkedin.com/pulse/data-types-variables-expressions-python-anubhav-kumar/
1. print(y)
2. print(y,y)
3. print(y+1)
4. Print(y+y)
5. Print(yy)
6. Options 2 & 3 & 4 & 5
7. None of the options
CMPT 120, Spring 2023, Mohammad Tayebi 19
In-class Exercise 3
• Arithmetic operators: + - * % ** / //
• % (mode or modulo) is remainder (11 % 3 is 2)
• Modding by 0 is an error, just like dividing by 0.
• ** is exponentiation (10**2 is 100)
• / is floating point division (3/2 is 1.5)
• // is integer division (3//2 is rounded down to 1).
CMPT 120, Spring 2023, Mohammad Tayebi 21
Operators 2
• Comparison operators: < <= == != >= >
• == tests if two values are equal
• != tests if two values are not equal
• 3 != 2 will be True, 3 == 2 will be False.
>>> x = 3
>>> Y = 2
>>> x = x * y
>>> x = x + 1
>>> print (x)
7
>>> a = 8
>>> b = 2
>>> c = a // b
>>> b = b ** b
>>> c += b
>>> print(c+1) 1. 9
2. 10
3. 11
4. 12
CMPT 120, Spring 2023, Mohammad Tayebi 25
In-class Exercise 6
>>> a = 1
>>> b = 2
>>> c = 1/2
>>> d = a + 1
>>> d = 12 / c
>>> print(d) 1. 21
2. 22
3. 23
4. 24
CMPT 120, Spring 2023, Mohammad Tayebi 26
In-class Exercise 7
a = 10
b = 30
c = a//b
d = b/c
e = a + b + c + d
print(e) 1. 40
2. 43
3. 44
4. Error
CMPT 120, Spring 2023, Mohammad Tayebi 27
Order of Operations
• String constants
• Anything inside of single quotes or double quotes, and + for strings is concatenation