1
APSC 258
Applications of Engineering Design
(2025 Term 2)
Dr. Jahangir Hossain
Sept. 4, 2019
About Python
How to Run Python?
There are several ways to run Python. Few options are given below
Option 1
1) Install Python from [Link], Install VS Code
3) In VS Code, install the Python extension (by Microsoft)
4) Write python Script using an editor and save [Link] file (say
[Link])
5) Open your .py file (say [Link]) and run using play sign button or
in command terminal by typing python [Link]
Option 2
Jupyter Notebook (.ipynb file)
The Jupyter Notebook is an open source web application that you can
use to create and share documents that contain live code, equations,
visualizations, and text. Follow posted instructions to setup this.
Option 3
Google Colab is an online Python environment that runs in your
browse, Go to [Link] and Sign in with a Google
account.
Basics and Syntax
• Variables (no declaration)
• Example Code
x=10
y=10.3
name='Jahangir'
sum=x+y
print(sum)
print(name)
• Basic data types: int, float, str, bool
Example
x=10
y=10.3
name= 'Jahangir'
name="Jahangir"
(Note: single quotes (') and double quotes (") are functionally the
same for defining strings)
• Comment: #
Example
# This code is to sum two numbers and print sum
Python List
• List (To store collection of items)
– Example
x=[3,5,6,2,7,9,10.5]
print(x) # This will print whole list
print(x[0])# This will print item at location index=0
(Note: Python uses 0-based indexing and also negative index)
print(x[1:4]) # This will print items 5,6,2
print(x[1:6:2])# x[start:end:step])-Slicing
start → where to begin (inclusive) and default is value 0
end / stop → where to stop (exclusive) and default value is end
step → how much to move each time and default value is 1
• lists have many built-in methods that let you add, remove, search, and modify elements in a
list.
– One example: [Link](8) #will append item 8 at the end
Python Tuples
• Tuples
– Example
y=(3,6,7)
print(y)
• lists and tuples are both used to store ordered collections of items, but they differ in
mutability, performance, and use cases.
– Lists are mutable (can be changed) but tuples are immutable (can not be changed)
– Example:
x=[5,6,8] # x is a list
x[1]=9 # item at location index 1 will be replaced by 9
y=(3,6,7) # y is a list
y[1]=9 # you will get an error (as tuples are immutable)
• Tuples are faster and require less memory
Python Dictionary
• Dictionary (a data structure used to store key–value pairs). Each item has a unique identifier
(key)
• Stores data as
– key : value
– Keys are unique and immutable
– Values can be any data type
– Unordered conceptually
Example
student1 = {
'name': 'John',
'age': 21,
'cgpa': 3.5}
print(student1)
print(student1['name'])
student1['age']=24 # will change age to 24
Unpacking and Swapping Values
• Unpacking is a way to assign elements of a collection to variables in a single statement
• Example
x=[3,5,1]
a,b,c=x # assigning items of x to a, b and c
print(a)
print(b)
output
3
5
• In Python, swapping values is extremely easy thanks to tuple unpacking — you don’t need a temporary variable like in other languages.
• Example Code
a = 5
b = 10
a,b = b,a #a takes existing value of b and b takes existing value of a
print(a, b)
output
10 5
• Example Code
a = 5
b = 10
c=20
a, b = b, a+c
print(a, b)
Arithmetic Operators
Assignment, Comparison, and Logical Operators
Assignment Operators Comparison Operators
Logical Operators
Control flow
• if/elif/else
x=2
if x>3:
y=x**2
print(x,y)
elif 3>x and x>0:
y=x**3
print(x,y)
else:
y=x+2
print(x,y)
Note: The colon (:) and whitespace in Python — these are critical for Python syntax, especially because Python uses indentation instead of braces {}.
• The colon tells Python that a new block of code is starting.
• Every block that follows a colon must be indented.
What is wrong in the following code?
x=2
if x>3:
y=x**2
print(x,y)
elif 3>x and x>0:
y=x**3
print(x,y)
else:
y=x+2
print(x,y)
Control flow
• For loop
x=[1,3,5,6]
for a in x:
y=a**2
print(a,y)
Output
1 1
3 9
5 25
6 36
Try this
x=[1,3,5,6]
for a in x:
y=a**2
print(a,y)
Output
Control flow
• While loop
x=0
while x<3:
y=x**2
print(x,y)
x=x+1
Output
0 0
1 1
2 4
Try this
x=0
while x<3:
y=x**2
print(x,y)
x=x+1
Output
Learning Through Coding Writing: Example 1
Write python code that calculates square of even numbers and cube of odd numbers. Assume
numbers run from 1 to 5. Use for loop.
numbers=range(1,6) # generating sequence 0 to 6
for num in numbers: # for loop to go through each number
if num % 2 == 0: # if statement to check if the number is even
y=num**2 # square
print(num, y)
else:
y=num**3
print(num, y)
Output
1 1
2 4
3 27
4 16
5 125
Learning Through Coding Writing: Example 2
Write python code that calculates square of even numbers and cube of odd numbers. Assume
numbers run from 1 to 5. Use while loop
num=1
while num <6: # check and loop if number is less than 5
if num % 2 == 0: # if statement to check if the number is even
y=num**2 # square
print(num, y)
else:
y=num**3 # cube
print(num, y)
num=num+1 # increment number by 1
Output
1 1
2 4
3 27
4 16
5 125