Introduction
to Python
section 1
By
Rhma Yasser
Grades:
• Attendence (5)
• Tasks & Quizes (10)
• Project – Practical (15)
Note: Don’t enter the class after 15 minutes.
Table of content
• Introduction to python
• Input – Output
• Comments
• Variables & Data types
• Numbers
• Strings
• Boolean & Comparison operators
• Loops
• If statement
• Functions
Resources:
1. Python crash course book
https://khwarizmi.org/wp-content/uploads/2021/04/Eric_Matthes_Python_Crash_Course_A_Hands.pdf
2. Python documentation
https://docs.python.org/3/tutorial/index.html
3. Geeks for Geeks
https://www.geeksforgeeks.org/python-programming-language-tutorial/
4. W3 schools
https://www.w3schools.com/python/
Other:
YouTube playlists
https://www.youtube.com/watch?v=h3VCQjyaLws&list=PLuXY3ddo_8nzrO74UeZQVZOb5-wIS6krJ
https://www.youtube.com/watch?v=mvZHDpCHphk&list=PLDoPjvoNmBAyE_gei5d18qkfIe-Z8mocs
Coursera
https://www.coursera.org/specializations/python-3-programming
01
Introduction
to Python
Jupyter Installation:
https://www.youtube.com/watch?v=WUeBzT43JyY
https://www.youtube.com/watch?v=8YGPfGDYAgI
What is python?
1. Python is a high-level, interpreted programming language
known for its simplicity and readability.
2. Python is an easy language to learn because of its simple
syntax.
3. It is widely used in various domains such as web
development, data science, artificial intelligence, machine
learning, and more
Why is python?
1. Community and Documentation: Python has a large and
active community of developers who contribute to its growth
and development.
2. Extensive Libraries: Python has a vast ecosystem of
libraries and frameworks for various purposes, allowing
developers to leverage existing tools for their projects.
3. Versatility: Python can be used for a wide range of
applications, from web development to scientific computing
and machine learning.
01
Input - Output
Input - Output
How the input() Function Works
message = input(“Enter Hello World")
How the print() Function Works
print(“Hello World")
02
Comments
Comments
In Python, the hash mark (#) indicates a comment. Anything following a
hash mark in your code is ignored by the Python interpreter.
#Say hello to everyone.
print("Hello Python people!")
03
Variables &
Data Types
Naming and Using Variables
1. Variable names can contain only letters, numbers, and underscores. They
can start with a letter or an underscore, but not with a number. For
instance, you can call a variable message_1 but not 1_message.
2. Spaces are not allowed in variable names, but underscores can be used
to separate words in variable names. For example, greeting_message
works, but greeting message will cause errors.
3. Avoid using Python keywords and function names as variable names.
4. Variable names should be short but descriptive. For example, name is
better than n, student_name is better than s_n, and name_length is
better than length_of_persons_name.
Variable types
1. Integer (int)
2. Float (float)
3. String (str)
4. Boolean (bool)
5. Complex (complex)
6. [...]
A variable is assigned using the = operator
Variable types cont..
• You can always check the type of a variable using the
type() function.
• Variables can be cast to a different type.
Numbers -> Integers & Floats
The arithmetic operators:
• Addition: +
• Subtract: -
• Multiplication: *
• Division: /
• Power: **
• Modulus: %
A quick note on the increment operator shorthand
Python has a common idiom that is not necessary, but
which is used frequently and is therefore worth noting:
x += 1
Is the same as:
x=x+1
This also works for other operators:
x += y adds y to the value of x
x *= y multiplies x by the value y
x -= y subtracts y from x
x /= y divides x by y
Underscores in Numbers & Multiple Assignment
• When you’re writing long numbers, you can group digits
using underscores to make large numbers more readable:
• You can assign values to more than one variable using just
a single line:
Constants
• When A constant is like a variable whose value stays the
same throughout the life of a program.
• Python doesn’t have built-in constant types, but Python
programmers use all capital letters to indicate a variable
should be treated as a constant and never be changed:
Operations on Strings
Operations on Strings Cont..
Operations on Strings Cont..
Adding Whitespace to Strings with Tabs or Newlines
Indexing & Slicing
Using Variables in Strings
In some situations, you’ll want to use a variable’s value inside a string.
For example, you might want two variables to represent a first name and a last name respectively, and
then want to combine those values to display someone’s full name:
04
Boolean &
Comparison
operators
Boolean & Comparison Operator
Boolean operators are useful when Comparison Operators:
making conditional statements, we
will cover these in-depth later. Greater than: >
Lesser than: <
and Greater than or equal to: >=
or Lesser than or equal to: <=
not Is equal to: ==
05
If Statement
Conditional Tests
Checking for Equality Checking for Inequality Checking Multiple Conditions
Using and to Check Multiple Conditions
Ignoring Case When Checking for Equality
if-else statement
For example, consider an amusement park that
charges different rates for different age groups:
• Admission for anyone under age 4 is free.
• Admission for anyone between the ages of 4
and 18 is $25.
• Admission for anyone age 18 or older is $40.
06
Loops
While loop
With the while loop we can
execute a set of statements as
long as a condition is true.
The while loop requires relevant
variables to be ready, in this
example we need to define an
indexing variable, i, which we
set to 1.
For loop
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple,
set etc.
for variable in iterable:
# Code block to execute
variable: This is the name you assign to the current item in the
iteration. In each iteration, it takes on the value of the next item in the
iterable.
Iterable: This can be any Python object that can return its elements one
at a time, such as a list, tuple, string, or dictionary.
Code block: This is the indented code that will be executed for each
item in the iterable.
Range
The range(start, stop, step) function is
often used to define x. The starting value
is defined by start, the final value is
defined by stop – 1, and the magnitude
at which x changes between loops is
defined by step
Enumerate
The enumerate() function in Python is used to
iterate over a sequence (like a list or a string)
while keeping track of the index of the current
item. This is especially useful when you need both
the item and its index during iteration.
for index, value in enumerate(iterable):
# Code block to execute
index: This will hold the current index of the item
in the iterable.
value: This will hold the current item from the
iterable.
iterable: This can be any sequence, such as a
list, tuple, or string.
Continue Break and Pass Statement
The continue Statement:
With the continue statement we can stop the current
iteration of the loop, and continue with the next
The break Statement
With the break statement we can stop the loop
before it has looped through all the items
The pass Statement
for loops cannot be empty, but if you for some
reason have a for loop with no content, put in the
pass statement to avoid getting an error.
07
Functions
Creating a Function
In Python a function is defined using Adding Arguments:
the def keyword:
Arbitrary Arguments, *args & Keyword Arguments
If you do not know how many arguments that will be
passed into your function, add a * before the parameter
name in the function definition.
This way the function will receive a tuple of arguments,
and can access the items accordingly:
Keyword Arguments
You can send arguments with the key = value syntax.
This way the order of the arguments does not matter.
Arbitrary Keyword Arguments, **kwargs
If you do not know how many keyword arguments that will be passed into your function, add
two asterisk: ** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items
accordingly:
Default Parameter Value
If we call the function without argument, it uses the default value:
Return value
Lambda Expression
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one
expression.
Syntax
lambda arguments : expression
The expression is executed and the result is returned:
Lambda Expression
Lambda Expression Cont..
Multiply argument a with argument b and return the
result:
Example
Add 10 to argument a, and
return the result:
Thank You