0% found this document useful (0 votes)
52 views

01-b - Python

Python is a high-level programming language created by Guido van Rossum in 1991. It is an interpreted language with features that make it easy to read and write programs. To run a Python program, code is written in a text file with a .py extension and executed using the Python interpreter. Common errors that can occur include syntax errors detected by the interpreter, runtime errors from illegal operations, and logic errors where a program runs but produces incorrect output.

Uploaded by

Omar AJ
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

01-b - Python

Python is a high-level programming language created by Guido van Rossum in 1991. It is an interpreted language with features that make it easy to read and write programs. To run a Python program, code is written in a text file with a .py extension and executed using the Python interpreter. Common errors that can occur include syntax errors detected by the interpreter, runtime errors from illegal operations, and logic errors where a program runs but produces incorrect output.

Uploaded by

Omar AJ
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

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

“Computers are good at following instructions, but not at


reading your mind.” –D. Knuth

"Programming is not a spectator sport." - Bill Young

Python 2
What is Python?

Python is a high-level programming language developed by Guido


van Rossum in the Netherlands in the late 1980s. It was released
in 1991.
Python has twice
received recognition as
the language with the
largest growth in
popularity for the year
(2007, 2010).
It’s named after the
British comedy troupe
Monty Python.

Python 3
What is Python?

Python is a simple but powerful scripting language. It has


features that make it an excellent first programming language.

Easy and intuitive mode of interacting with the system.


Clean syntax that is concise. You can say/do a lot with
few words.
Design is compact. You can carry the most
important language constructs in your head.
There is a very powerful library of useful functions
available.

You can be productive quite quickly. You will be spending more


time solving problems and writing code, and less time grappling
with the idiosyncrasies of the language.

Python 4
What is Python?

Python is a general purpose programming language. That means


you can use Python to write code for any programming tasks.

Python was used to write code


for: the Google search engine
mission critical projects at NASA
programs for exchanging financial transactions at the NY
Stock Exchange
the grading scripts for this class

Python 5
What is Python?

Python can be an object-oriented programming language.


Object-oriented programming is a powerful approach to
developing reusable software.

Python is interpreted, which means that Python code


is translated and executed one statement at a time.

This is different from other languages such as C which


are
compiled, the code is converted to machine code and
then the
program can be run after the compilation is finished.

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

To install Python on your personal computer / laptop, you


can download it for free at: www.python.org/downloads
There are two major versions: Python 2 and Python 3.
Python 3 is newer and is not backward compatible
with Python 2. Make sure you’re running Python last
stable version.
It’s available for Windows, Mac OS, Linux.
It comes with an editor and user interface called IDLE.
I strongly recommend downloading and installing the
PyCharm, Educational version, IDE.

Python 8
A Simple Python Program: Interactive Mode

This illustrates using Python in interactive mode from the


command line. Your command to start Python may be
different.

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

This submits the program in file my_first_program.py to


the Python interpreter to execute.
This is better, because you have a file containing your program
and you can fix errors and resubmit without retyping a bunch of
stuff.

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:

Notice that if you’re computing an expression in interactive


mode,
it will display the value without an explicit print.
Python will figure out the type of the value and print
it appropriately. This is very handy when learning the
basics of computations in Python. Python 12
Encoding

 Encoding is a system or standard that dictates


what "thing" is representing by what number
 Example ASCII or UTF-8
 This number represents this character
 First 128 numbers of ASCII and UTF-8 same
 32 -> space character
 65 -> capital A
 97 -> lower case a
 48 -> digit 0

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

Define your program in


file
Filename.py:
d ef main ( ) : Defining a function called main.
Python statement
Python statement These are the instructions that make
Python statement
... up your program. Indent all of them the
Python statement same amount (usually 4 spaces).
Python statement
Python statement

main ( ) This says to execute the function


main.
To run it:
> python file_name.py This submits your program in
file_name.py to the Python
interpreter.

Python 15
Aside: Running Python From a File

Typically, if your program is in file hello.py, you can run your


program by typing at the command line:

> python hello.py

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

Comments shall also be interspersed in your code:


Before each function or class definition (i.e., program
subdivision);
Before each major code block that performs a significant task;
Before or next to any line of code that may be hard to
understand.

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

Comments are useful so that you and others can understand


your code. Useless comments just clutter things up:

x = 1 # assign 1 to x
y = 2 # assign 2 to y

Python 19
Programming Style

Some Python programming conventions:


Follow variable and function naming conventions.
Use meaningful variable/function names.
Document your code effectively.
Each level indented the same (4 spaces).
Use blank lines to separate segments of code inside functions.
2 blank lines before and after every function.

We’ll learn more elements of style as we go.

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

runtime errors: you try something illegal while your code


is executing

>>> 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

logic errors: your program runs but returns an incorrect


result.
>>> prod = 0
>>> f o r x i n r a n g e [ 1 , 6 ] :
... prod * = x
> > > pr i nt
( pr od ) 0

This program is syntactically fine and runs without error. But


it probably doesn’t do what the programmer intended; it
always returns 0 no matter the values in range. How would
you fix it?
Logic errors are often the hardest errors to find and fix.

Python 23

You might also like