CSE111 Lecture2 F
CSE111 Lecture2 F
Lecture 2
Python Scripts
• Interactive Python is good for experiments and programs of 3-4 lines long
• But most programs are much longer so we type them into a file and tell
python to run the commands in the file.
◦IPython
◦Jupyter Notebooks
Good for: Quick testing, Learning and experimenting, Small code snippets
Script Mode
You write your Python code in a file (ending with .py) and run the whole file at once.
•Typically done in an editor (like VS Code, PyCharm) or even simple text editors.
•VS Code stands for Visual Studio Code — it’s a free, open-source code editor made by
Microsoft. (Cross-platform — works on Windows, macOS, and Linux)
•Built-in features: Syntax highlighting (colors for code), Autocompletion (suggestions while
you type), Error checking, Debugging (finding and fixing errors)
Script Mode is good for: Larger programs, Projects, Anything you want to save and reuse
Program Steps and Program Flow
x=2 Program:
Output:
print(x) x = 2
print(x) 2
x=x+2 x = x + 2 4
print(x)
print(x)
print('Smaller') Program:
No Output:
x = 5
Yes if x < 10: Smaller
x > 20 ? print('Smaller') Finish
if x > 20:
print('Bigger') print('Bigger')
No
print('Finish')
print('Finish')
Repeated Steps
n=5
No Yes Output:
n>0? Program:
5
print(n) n = 5 4
while n > 0 :
print(n)
3
n = n -1 n = n – 1 2
print('Blastoff!') 1
Blastoff!
Loops (repeated steps) have iteration variables that
print('Blastoff')
change each time through a loop.
When you start Python, you will see something
like:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
9
Python Programming, 3/e
Elements of Python
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def',
'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Statements
x = 12.2 x 12.2
y = 14
y 14
Variables
• A variable is a named place in the memory where a programmer can store
data and later retrieve the data using the variable “name”
hours = 35.0
What are these bits rate = 12.50
of code doing? pay = hours * rate
print(pay)
Assignment Statements
x = 3.9 * x * ( 1 - x )
x 0.6
A variable is a memory location
used to store a value (0.6)
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
0.6 0.6
x = 3.9 * x * ( 1 - x )
A variable is a memory location used to
store a value. The value stored in a 0.4
variable can be updated by replacing the
old value (0.6) with a new value (0.936).
0.936
The right side is an expression. Once the
expression is evaluated, the result is
placed in (assigned to) the variable on the
left side (i.e., x).
Expressions…
Numeric Expressions
3
Order of Evaluation
• When we string operators together - Python must know which one
to do first
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:
– Left to right
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11.0 1 + 8 / 4 * 5
>>>
1 + 2 * 5
Parenthesis
Power
Multiplication 1 + 10
Addition
Left to Right 11
Operator Precedence
Parenthesis
• Remember the rules top to bottom Power
Multiplication
Addition
• When writing code - use parentheses Left to Right
35
Python Programming, 3/e