py
py
1
What is Language?
Or:
>>> 2 + 2
The output is:
4
Syntax
Python is known for its clean syntax.
Semicolons:
def my_function():
print('First command’)
name = ‘Ali’
Name = ‘Mona'
Syntax
Comments
A = “Ali”
print(A)
Comments
Comments are any text to the right of the # symbol and is
mainly useful as notes for the reader of the program.
For example:
#calculates the sum of any given two numbers
a+b
Print('hello world’) # Note that print is a
statement
Data Types
• In programming, a data type consists of a set of
values and a set of operations that can be performed
on those values. A literal is the way a value of a data
type looks to a program.
Data Types
• Python interpreter evaluates a literal, the value it
returns is simply that literal. Table 2-2
The backslash symbol (\) signifies that the character that follows it is
a control code, not a literal character.
Control Codes (Escape Sequences)
Control Codes within Strings
The \n control code represents the newline control code which moves
the text cursor down to the next line in the console window.
\t for tab,
\b for backspace,
print('1\a2\a3\a4\a5\a6')
Control Codes within Strings
print("Did you know that 'word' is a word?")
age=32
print(name)
print(age)
Variables
In summary, variable names:
• Are Case sensitive: time and TIME are not the same
print(x)
x = 20
the output:
x = 100 y = -45 z = 0
Types
Determining the Type
• Just use the type() function and pass the variable of your choice as
an argument, like the example below.
type(my_variable)
print(type(my_variable))
Types
Boolean:
my_bool = True
print(type(my_bool))
<class 'bool'>
Types
Numbers
• Integer
my_int = 32
print(type(my_int))
<class 'int'>
Types
Numbers
• Float
my_float = 32.85
print(type(my_float))
<class 'float'>
String Concatenation
• String
You can join two or more strings to form a new string using the
concatenation operator +
.Here is an example:
word2 = 'York'
print(word1 + word2)
Output = NewYork
Python
Wadhah S. Sebti
Expressions
Wadhah S. Sebti
Selection
Selection: if and if-else Statements
In this section, we explore several types of selection statements, or
control statements, that allow a computer to make choices.
Yet another way to describe this situation is to say that if the number is
greater than or equal to 0 and less than or equal to 100, then we want the
program to perform the computations and output the result; otherwise, it
should output an error message. The logical operator and can be used to
construct a different compound Boolean expression to express this logic:
Selection
Logical Operators and Compound Boolean Expressions
else:
Wadhah S. Sebti
PROBLEM SOLVING
Python Assignment Operators:
Wadhah S. Sebti
Loops
Python Assignment Operators:
Loops
Python Assignment Operators:
Loops
Python Assignment Operators:
Loops
Python Membership Operators:
Loops
Definite Iteration:
Loops are used when you need to repeat a block of
code a certain number of times or apply the same logic
over each item in a collection.
Basic Syntax:
The list of cars contains three items.The for loop will iterate
over the list and store each item in the car variable,
and then execute a statement, in this case print(car), to
print each carin the console.
Loops
>>> for character in "Hi there!":
Hi there!
Loops
For Loop (range() function):
print(“Hello!")
Hello!
Hello!
Hello!
Hello!
Loops
•We can print the output statement four times at one line:
>>> exponent = 3
>>> product = 1
248
Loops
You can also definea start and stop using range().
1 2 3 4 5 6 7 8 9 10
Loops
Here we are starting in 5 and stoping in 10.The number
you set to stop is not included.
Loops
Here we starting in 10 and incrementing by 2 until 20,
since 20 is the stop, it is not included.
Loops
Loops That Count Down
>>> for count in range(2, 11, 2):
print(count, end = " ")
246810
>>> theSum = 0
>>> for count in range(2, 11, 2):
theSum += count
>>> theSum
30
Loops
Loops That Count Down
>>> for count in range(10, 0, -1):
10 9 8 7 6 5 4 3 2 1
Loops
Augmented Assignment
Expressions such as x = x + 1 or x = x + 2 occur so frequently in loops
that Python includes abbreviated forms for them. The assignment
symbol can be combined with the arithmetic and concatenation
operators to provide augmented assignment operations.
<variable> <operator>= <expression>
which is equivalent to
<variable> = <variable> <operator> <expression>
Loops
Augmented Assignment
Following are several examples:
a = 17
s = "hi"
a += 3 # Equivalent to a = a + 3
a -= 3 # Equivalent to a = a - 3
a *= 3 # Equivalent to a = a * 3
a /= 3 # Equivalent to a = a / 3
a %= 3 # Equivalent to a = a % 3
s += " there" # Equivalent to s = s + " there"
Loops
w h i l e Loops:
Basic Syntax:
The basic syntax of a while loop is as below.
Loops
w h i l e Loops:
The example below takes each value of number and
calculates its squared value.
Loops
theSum = 0.0
number = float(data)
theSum += number
# Counting down with a for loop # Counting down with a while loop
Wadhah S. Sebti
The Structure of Strings
Unlike an integer, which cannot be decomposed into more primitive
parts, a string is a data structure. A data structure is a compound unit
that consists of several other pieces of data. A string is a sequence of
zero or more characters. Recall that you can mention a Python string
using either single quote marks or double quote marks. Here are some
examples:
The Structure of Strings
>>> "Hi there!"
'Hi there!'
>>> ""
''
>>> 'R'
'R'
Note that the shell prints a string using single quotes, even when you
enter it using double quotes. In this book, we use single quotes with
single-character strings and double quotes with the empty string or
with multi-character strings.
The Structure of Strings
When working with strings, the programmer sometimes must be aware
of a string’s length and the positions of the individual characters within
the string. A string’s length is the number of characters it contains.
Python’s len function returns this value when it is passed a string.
>>> len("Hi there!")
9
>>> len("")
0
The Structure of Strings
The positions of a string’s characters are numbered from 0, on the left,
to the length of the string minus 1, on the right. Figure 4-1 illustrates
the sequence of characters and their positions in the string "Hi there!".
Note that the ninth and last character, '!', is at position 8.