EE 1021
Programming for
Engineers
Introduction
1
Instructors
Instructor Lab Assistant
• Basri ERDOĞAN • Ahmet Aytug AYRANCI
• Office: 2D-07 • Office: 2D-07
• E-mail: [email protected] • E-mail:
• Monday 10:00-12:00
• Wednesday 13:00-15:00
2
Welcome to Computer
Programming course!
• We will learn to program in Python.
• We assume no prior knowledge.
• However, we advance fast.
• Hard work is required!
• The only way to keep on track is to practice!
• Goal: enable you to use programming as a tool to
solve “real world” problems.
3
Theory Session
• In a classroom
• Introduce new concepts
• Might be hard to grasp in the beginning
Practice Session (Lab)
• Maximum number of students: 20
• Hands-on
• There will be graded tasks
• Attendance is mandatory (will fail if you miss more than 2 lab
sessions!)
5
WEEKLY SCHEDULE & TEACHING- LEARNING METHODS
Week Subjects Teaching and learning methods
1 Introduction to Course and Python. Variables: Numbers, strings Presentation and practice
Presentation and practice
2 If-Else Statement, Loops: For and While statements
3 Built-in functions and new function declarations Presentation and practice
4 Data Structures (Lists, Tuples, Sets, Dictionaries) Presentation and practice
5 Errors and Exceptions Presentation and practice
6 Numpy Toolbox Presentation and practice
7 Plotting and Visualization Presentation and practice
8 Midterm
9 Object Oriented Programming Presentation and practice
10 Object Oriented Programming II Presentation and practice
11 Input and Output (Output formatting, File reading/writing) Presentation and practice
12 Introduction to Matlab (matrices, arrays and indexing) Presentation and practice
13 Matlab data structures, control flow tools (If-Else, for and while statements) Presentation and practice
14 Drawing and visualization with Matlab Presentation and practice
Grading
• Lab Work : 15%
• Homework: 25%
• Midterm : 25%
• Written or on CATs
• Final : 35%
• Written or on CATs
7
Course Materials
• Lecture notes / Codes
• Homework assignments
• Midterm and Final Exams
• References:
• Python 3.8 tutorial: http://docs.python.org/3.8/tutorial
• Think Python, by Allen B. Downey
(http://greenteapress.com/thinkpython/thinkpython.html)
8
Code development
• Package management tool
• Anaconda
• IDE (Integrated Development Env.)
• Visual Studio Code, Spyder, PyCharm
9
ANACONDA
• Anaconda (Individual Edition) is a free, easy-to-install package
manager, environment manager, and Python distribution with a
collection of 1,500+ open source packages.
• Anaconda is platform-agnostic, so you can use it whether you are on
Windows, macOS, or Linux.
• Installation instructions for WINDOWS
https://docs.anaconda.com/anaconda/install/windows/
• Installation instructions for MAC
https://docs.anaconda.com/anaconda/install/mac-os/
10
18.09.2024 Python Programming - Lab 0 11
Spyder
12
Python Basics
WHAT DOES A COMPUTER DO
• Fundamentally:
• performs calculations
• a billion calculations per second!
• remembers results
• 100s of gigabytes of storage!
• What kinds of calculations?
• built-in to the language
• ones that you define as the programmer
• We will write recipes to make computer perform
certain tasks
A NUMERICAL EXAMPLE
• square root of a number x is y such that y*y = x
• recipe for deducing square root of a number x (16)
1) Start with a guess, g
2) If g*g is close enough to x, stop and say g is the answer
3) Otherwise make a new guess by averaging g and x/g
4) Using the new guess, repeat process until close enough
WHAT IS A RECIPE
1. sequence of simple steps
2. flow of control process that specifies when each
step is executed
3. a means of determining when to stop
1+2+3 = an algorithm!
COMPUTERS ARE MACHINES
• how to capture a recipe in a mechanical process
• fixed program computer
• calculator
• stored program computer
• machine stores and executes instructions
BASIC COMPUTER
ARCHITECTURE
STORED PROGRAM COMPUTER
• sequence of instructions stored inside computer
• built from predefined set of primitive instructions
1. arithmetic and logic
2. simple tests
3. moving data
• special program (interpreter) executes each
instruction in order
• use tests to change flow of control through sequence
• stop when done
PYTHON PROGRAMS
• A program is a sequence of definitions and
commands
• Definitions evaluated
• Commands executed by Python interpreter in a shell
• Commands (statements) instruct interpreter to do
something
• Can be typed directly in a shell or stored in a file
that is read into the shell and evaluated
OBJECTS (variables)
• programs manipulate data objects
• objects have a type that defines the kinds of things
programs can do to them
• objects can be
• scalar (cannot be subdivided)
• Int, float, bool
• array (multiple elements)
• String, list, numpy array
EXPRESSIONS
• combine objects and operators to form expressions
• an expression has a value, which has a type
• syntax for a simple expression
• <object> <operator> <object>
BINDING VARIABLES AND VALUES
(Assignment)
• Equal sign is an assignment of a value to a variable
name
pi = 3.14159
pi_approx = 22/7
• value stored in computer memory
• an assignment binds name to value
• retrieve value associated with name or variable by
invoking the name, by typing pi
Arithmetic Operators on
ints and floats
• x+y → the sum
• x-y → the difference
• x*y → the product
• x/y → the division
• x%y → the remainder when x is divided by y
• x**y → x to the power of y
Comparison Operators
Operator Name Description
x < y Less than true if x is less than y, otherwise false.
x > y Greater than true if x is greater than y, otherwise false.
Less than or true if x is less than or equal to y,
x <= y
equal to otherwise false.
Greater than or true if x is greater than or equal to y,
x >= y
equal to otherwise false.
x == y Equal true if x equals y, otherwise false.
x != y Not Equal true if x is not equal to y, otherwise false.
Logical Operators
Operator Description
Both True: True,
x and y
otherwise: False.
At least one is True: True,
x or y
Otherwise: False.
not x x is False → True, x is True → False
STRINGS
• letters, special characters, spaces, digits
• enclose in quotation marks or single quotes
hi = "hello there"
• concatenate strings
name = "ana"
greet = hi + name
greeting = hi + " " + name
• do some operations on a string as defined in
Python docs silly = hi + " " + name * 3
STRINGS – Indexing
• square brackets used to perform indexing into a string
to get the value at a certain index/position
s ="abc"
index: 0 1 2 indexing always starts at 0
index: -3 -2 -1 last element always at index -1
S[0] → returns "a"
S[1] → returns "b"
S[2] → returns "c"
S[3] → error: index out of bounds
S[-1] → returns "c"
S[-2] → returns "b"
S[-3] → returns "a"
STRINGS – Slicing
• can slice strings using [start:stop:step]
• if give two numbers, [start:stop],step=1 by
default
• you can also omit numbers and leave just colons
s = "abcdefgh"
s[3:6] → returns ”def”, same as s[3:6:1]
s[3:6:2] → returns ”df”
s[::] → returns ”abcdefgh”, same as s[0:len(s):1]
s[::-1] → returns ”hgfedcba”, same as s[-1:len(s)+1:-1]
s[4:1:-2] → returns ”ec”
INPUT/OUTPUT – print(" ")
• use to output stuff to console
• function name is print
x = 1
print(x)
x_str = str(x)
print("my fav num is", x, ".", "x =", x)
print("my fav num is " + x_str +". "+ "x = " + x_str)
INPUT/OUTPUT – input(" ")
1. prints whatever is in the quotes
2. user types in something and hits enter
3. assigns that value to a variable
text = input("Type anything... ")
print(5*text)
• input gives you a string so must cast if working with
numbers
num = int(input("Type a number... "))
print(5*num)