Python
Faisal Salaheldeen
Obour STEM School
What is Python?
• Python is a popular programming language. It was
created by Guido van Rossum, and released in
1991.
Why Python?
• Python works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the English
language.
• Python has syntax that allows developers to write
programs with fewer lines than some other programming
languages.
• Python runs on an interpreter system, meaning that code
can be executed as soon as it is written. This means that
prototyping can be very quick.
• Python can be treated in a procedural way, an object-
oriented way or a functional way.
• Good to know
Python Syntax
• For print data in python, use print function
• print("Hello, World!")
• Python Indentation
• Indentation refers to the spaces at the beginning of a
code line.
• Where in other programming languages the indentation
in code is for readability only, the indentation in Python is
very important.
• Python uses indentation to indicate a block of code.
• Example
• if 5 > 2:
print("Five is greater than two!")
• The number of spaces is up to you as a
programmer, but it has to be at least one.
• You have to use the same number of spaces in the
same block of code, otherwise Python will give you
an error
Python Variables
• In Python, variables are created when you assign a
value to it:
• Example
• Variables in Python:
• x=5
y = "Hello, World!"
Comments
• Python has commenting capability for the purpose
of in-code documentation.
• Comments start with a #, and Python will render the
rest of the line as a comment:
• Example
• Comments in Python:
• #This is a comment.
print("Hello, World!")
Python Data Types
• Built-in Data Types
• In programming, data type is an important
concept.
• Variables can store data of different types, and
different types can do different things.
• Python has the following data types built-in by
default, in these categories:
• In Python, the data type is set when you assign a
value to a variable.
Python Data Types
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Python Data Types
• Getting the Data Type
• You can get the data type of any object by using
the type() function:
• Example
• Print the data type of the variable x:
• x=5
print(type(x))
• The result is
• <class 'int'>
Python Numbers
• There are three numeric types in Python:
• int
• float
• complex
• Variables of numeric types are created when you
assign a value to them:
• Example
• x = 1 # int
y = 2.8 # float
z = 1j # complex
Python Numbers
• Complex
• Complex numbers are written with a "j" as the
imaginary part:
• Example
• Complex:
• x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Python Numbers
• The result will be:
• <class 'complex'>
<class 'complex'>
<class 'complex'>
Change variable types
• functions:
• int() - constructs an integer number from an integer
literal, a float literal (by removing all decimals), or a
string literal (providing the string represents a whole
number)
• float() - constructs a float number from an integer
literal, a float literal or a string literal (providing the
string represents a float or an integer)
• str() - constructs a string from a wide variety of data
types, including strings, integer literals and float
literals
Change variable types
• Example
• Strings:
• x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
• x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
• y = int(2.8) # y will be 2
z = int("3") # z will be 3
Python Strings
• Strings in python are surrounded by either single
quotation marks, or double quotation marks.
• 'hello' is the same as "hello".
• You can display a string literal with
the print() function:
• Example
• print("Hello")
print('Hello')
• Result will be
• Hello
Hello
Python Strings
• Multiline Strings
• You can assign a multiline string to a variable by
using three quotes:
• Example
• You can use three double quotes:
• a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Python Strings
• Strings are Arrays
• Example
• Get the character at position 1 (remember that the
first character has the position 0):
• a = "Hello, World!"
print(a[1])
• Result
• e
Python Strings
• Looping Through a String
• Since strings are arrays, we can loop through the
characters in a string, with a for loop.
• Example
• Loop through the letters in the word "banana":
• for x in "banana":
print(x)
• b
a
n
a
n
a
Python Strings
• String Length
• To get the length of a string, use the len() function.
• Example
• a = "Hello, World!"
print(len(a))
• Check String
• Example
• txt = "The best things in life are free!"
print("free" in txt)
• Result is: True
Python Strings
• Example
• txt = "The best things in life are free!"
print("expensive" not in txt)
• Result : True
Slicing Strings
You can return a range of characters by using the slice
syntax.
• Example
• b = "Hello, World!"
print(b[2:5])
• Result: llo
• Example
• Get the characters from the start to position 5 (not
included):
• b = "Hello, World!"
print(b[:5])
• Result: Hello
• Slice To the End
• Example
• b = "Hello, World!"
print(b[2:])
• Result: llo, World!
• Example
• b = "Hello, World!"
print(b[-5:-2])
• Result: orl
Modify Strings
• Upper Case
• Example
• a = "Hello, World!"
print([Link]())
• Result: HELLO, WORLD!
• Lower Case
• Example
• The lower() method returns the string in lower case:
• a = "Hello, World!"
print([Link]())
• Result: hello, world!
• Remove Whitespace
• Whitespace is the space before and/or after the
actual text, and very often you want to remove this
space.
• Example
• The strip() method removes any whitespace from
the beginning or the end:
• a = " Hello, World! "
print([Link]()) # returns "Hello, World!"
• Replace String
• Example
• a = "Hello, World!"
print([Link]("H", "J"))
• Returns: Jello, World!
• Split String
• a = "Hello, World!"
print([Link](",")) # returns ['Hello', ' World!']
• Returns: ['Hello', ' World!']
• Merging variables
• a = "Hello"
b = "World"
c=a+b
print(c)
• Returns: HelloWorld
• we cannot combine strings and numbers using +
• But we can combine strings and numbers by using
the format() method!
• Example
• quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print([Link](quantity, itemno, price))
• Returns: I want 3 pieces of item 567 for 49.95 dollars.
• You can use index numbers {0} to be sure the
arguments are placed in the correct placeholders:
• Example
• quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of
item {1}."
print([Link](quantity, itemno, price))
• Returns: I want to pay 49.95 dollars for 3 pieces of
item 567
• Escape Characters
• To insert characters that are illegal in a string, use an
escape character.
• An escape character is a backslash \ followed by
the character you want to insert.
• Examples
• txt = "We are the so-called \"Vikings\" from the
north.“
• Results: We are the so-called "Vikings" from the
north.