Lecture 02 - Intro to Python 2
Lecture 02 - Intro to Python 2
Programming
Simple Python Programming
1
Overview
●Introduce Python programming
● Pre se nt e xample s illustrating ke y language fe ature s
2
IDEs
3
Statements
4
Variables
7 is a value
x is a variable
6
Python Style
7
Variable names
8
Types
A function performs a task when you call it by writing its name, followed by
parentheses, (). The parentheses contain the function’s argument—the data that the
type function needs to perform its task.
9
Arithmetic
10
Exception and Trackbacks
11
Grouping expressions with parentheses
12
Operator precedence rules
https://docs.python.org/3/reference/expressions.html#operat
or-pre ce de nce
13
Operand types
●If both ope rands are inte ge rs, the re sult is an inte ge r—
e xce pt for the true -division (/) ope rator, which always
yie lds a floating-point numbe r.
● If both ope rands are floating-point numbe rs, the re sult is a
floating-point numbe r.
● Expre ssions containing an inte ge r and a floating- point
numbe r are mixed-type expressions —the se always
produce floating-point numbe rs.
14
Function print and an Intro to Single- and
Double- Quoted Strings
When a backslash (\) appears in a string, it’s known as the escape character.
\n represents the newline character escape sequence, which tells print to move
the output cursor to the next line. 15
Python programmers
generally prefer
single quotes.
16
Other Escape Sequences
17
Printing the Value of an Expression
18
Ignoring a Line Break in a Long String
You may also split a long string (or a long statement) over
se ve ral line s by using the \ continuation character as the
last characte r on a line to ignore the line bre ak:
19
Triple-Quoted Strings
20
Multiline Strings
21
Getting Input from the User
22
Function input Always Returns a String
23
Getting an Integer from the User
24
Decision Making: The if Statement and
Comparison Operators
●A condition is a Boole an e xpre ssion with the value True
or False.
● True and False are ke ywords
25
comments
Docstrings
The Style Guide for
Python Code states that
each script should start
with a docstring that
explains the script’s
purpose
Splitting a lengthy
statement across lines
26
Chaining Comparisons
27
Precedence of the Operators
28
Objects and Dynamic Typing
29
Variables Refer to Objects
30
the variable x re fe rs to the inte ge r obje ct containing 7
31
Dynamic Typing
33