01-b - Python
01-b - Python
2022 - 2023
Python 1
Some Thoughts about Programming
“The only way to learn a new programming language is by writing
programs in it.” –B. Kernighan and D. Ritchie
Python 2
What is Python?
Python 3
What is Python?
Python 4
What is Python?
Python 5
What is Python?
Python 6
The Interpreter
Actually, Python is always translated into byte code, a lower level
representation.
The byte code is then interpreted by the Python Virtual Machine.
Python 7
Getting Python
Python 8
A Simple Python Program: Interactive Mode
Here you see the prompt for the OS/command loop for the
Python interpreter read, eval, print loop.
Python 9
A Simple Python Program: Script Mode
Here’s the “same” program as I’d be more likely to write it.
Enter the following text using a text editor into a file called, say,
MyFirstProgram.py. This is called script mode.
In file my_first_program.py:
Python 10
A Simple Python Program
Python 11
Aside: About Print
If you do a computation and want to display the result use
the print function.
You can print multiple values with one print statement:
Python 13
Computer Memory
Recall, 1 bit -> a single 0 or 1
1 byte = 8 bits
A typical laptop or desktop circa 2021
… has 4 to 16 Gigabytes of RAM, also known
as main memory.
1 Gigabyte -> 1 billion bytes
The programs that are running store their
instructions and data (typically) in the RAM
… have 100s of Gigabytes up to several
Terabytes (trillions of bytes) in secondary
storage. Long term storage of data, files
Typically spinning disks or solid state drives.
Python 14
The Framework of a Simple Python Program
Python 15
Aside: Running Python From a File
Python 16
Program Documentation
Documentation refers to comments included within a source
code file that explain what the code does.
Include a file header: a summary at the beginning of each
file explaining what the file contains, what the code does,
and what key feature or techniques appear.
You shall always include your name, email, grader,
and a brief description of the program.
# File: <NAME OF FILE>
# Description: <A DESCRIPTION OF YOUR PROGRAM>
# Assignment Number: <Assignment Number, 1 - 13>
#
# Name: <YOUR NAME>
# EID: <YOUR EID>
# Email: <YOUR EMAIL>
# Grader: <YOUR GRADER'S NAME Carolyn OR Kaitlyn OR Yugam OR Yundi>
#
# On my honor, <YOUR NAME>, this programming assignment is my own work
# and I have not provided this code to any other student.
Python 17
Program Documentation
sum = 0
# s um t he i nt e ge r s [ s t a r t . . . e nd
] f or i i n r a nge ( s t ar t , e nd +
1) :
sum += i
Python 18
Don’t Over Comment
x = 1 # assign 1 to x
y = 2 # assign 2 to y
Python 19
Programming Style
Python 20
Errors:
Syntax
We will encounter three types of errors when developing
our Python program.
syntax errors: these are ill-formed Python and caught by the
interpreter
prior to executing your code.
>>> 3 = x
Fi l e " < s t di n > " , l i ne 1
Synt a x Er r or : c an ’ t a s s i gn t o
l i t er al
These are typically the easiest to find and fix.
Python 21
Errors: Runtime
>>> x = 0
>>> y = 3
>>> y / x
Tr a c e ba c k ( mos t r e c e nt c a l l l a s t ) :
Fi l e " < s t di n > " , l i ne 1 , i n < modul e
>
Ze r o Di vi s i on Er r or : di vi s i on by z e r o
Python 22
Errors: Logic
Python 23