PFE_1021_Th_Week1_n
PFE_1021_Th_Week1_n
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:
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!
• Hands-on
1 Introduction to Course and Python. Variables: Numbers, strings Presentation and practice
Presentation and practice
2 If-Else Statement, Loops: For and While statements
11 Input and Output (Output formatting, File reading/writing) Presentation and practice
13 Matlab data structures, control flow tools (If-Else, for and while statements) Presentation and practice
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
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.
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 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 Description
print(5*text)