You are on page 1of 34

Section 1

Introduction to
Python programming language

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


1
MOHAMED MOWAFY
Topics
o What is Python?
o Python Syntax
o Download, install Python and creating Python file
o Python Command Line
o printing output
o Python Variables and Data Types
o Python Indentation
o Python Conditions
o If statements

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


2
MOHAMED MOWAFY
What is Python?
Python is a popular programming language, It is used for:
o Creating workflows.
o Creating web applications on a server.
o Connect to database systems.
o Read and modify files.
o Handle big data and perform complex mathematics.

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


3
MOHAMED MOWAFY
Why Python?
o Python works on different platforms (Windows, Mac, Linux).
o Python has a simple syntax similar to the English language.
o Python has syntax that allows developers to write programs
with fewer lines than some other programming languages.
o Python runs on an interpreter system, meaning that code
can be executed as soon as it is written.
o Python can be treated in a procedural way, an object-
oriented way or a functional way.

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


4
MOHAMED MOWAFY
Python Syntax
o Python has some similarities to the English language with
influence from mathematics.
o Python uses new lines to complete a command, as
opposed to other programming languages which often
use semicolons.
o Python relies on indentation, using whitespace, to define
scope; such as the scope of loops, functions and classes,
Other programming languages often use curly-brackets
for this purpose.

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


5
MOHAMED MOWAFY
Download and install Python
https://www.python.org
https://www.python.org/downloads/

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


6
MOHAMED MOWAFY
Creating Python file
1- write code in notepad++ or visual studio code or any
editor
2- save as -----.py
3- run python -----.py in terminal.
python ex1.py

Or use visual studio code to write code and run in terminal.


https://code.visualstudio.com

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


7
MOHAMED MOWAFY
Python Command Line
To test a short amount of code in python sometimes it is
quickest and easiest not to write the code in a file, write in
python command line.
1- open terminal
2- python or py to start python command line

3- write code then press enter


4- exit() to return to terminal and exit python command line

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


8
MOHAMED MOWAFY
printing output
Print() used to print result

Syntax:
print("")

Ex:
print("Hello python")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


9
MOHAMED MOWAFY
Comments
# to add comment line
""" to add multiline comment ""“
Ex:
#This is a comment
"""
This is a comment
written in
more than one line
"""

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


10
MOHAMED MOWAFY
Python Variables
o Variables are containers for storing data values.
o Python has no command for declaring a variable,
variables are created when you assign a value to it.
o Variables do not need to be declared and can even
change type after they have been set.
o String variables can be declared either by using single or
double quotes.
o To concatenate or combine two strings you can use the +
operator.

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


11
MOHAMED MOWAFY
Rules for Python variables name
o variable name must start with a letter or underscore.
o variable name cannot start with a number.
o variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, _ ).
o variable names are case-sensitive (age, Age and AGE are
three different variables).
o variable name cannot be any of the Python keywords.

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


12
MOHAMED MOWAFY
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
Binary Types: bytes, bytearray

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


13
MOHAMED MOWAFY
Assigning variables values
Ex

x=10
nam="Mohamed"
city='Cairo'
age=46.5

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


14
MOHAMED MOWAFY
Casting
If you want to specify the data type of a variable, this can be
done with casting.

Ex
x = str(3) # x will be '3'

y = int(3) # y will be 3

z = float(3) # z will be 3.0

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


15
MOHAMED MOWAFY
Input()
used to input data to Python program
Syntax
input("")

Ex
number = input("Enter a number: ")
print("You entered:", number)

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


16
MOHAMED MOWAFY
Python Indentation
o Python uses indentation (space at beginning of code line)
to indicate a block of code.
o The number of spaces is up to you, the most common use
is four, but it has to be at least one.
o You have to use the same number of spaces in the same
block of code.
Ex
if 5 > 2:
print("Five is greater than two!")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


17
MOHAMED MOWAFY
Python Conditions
Equals: a == b
Not Equals: a != b
Less than: a<b
Less than or equal to: a <= b
Greater than: a>b
Greater than or equal to: a >= b

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


18
MOHAMED MOWAFY
If statements
Python relies on indentation (whitespace at the beginning
of a line) to define scope in the code.

Ex
a = 200
b = 33
if b > a:
print("b is greater than a")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


19
MOHAMED MOWAFY
If elif else statement
Ex

a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")t("b is greater than a")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


20
MOHAMED MOWAFY
Nested If
Ex

x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


21
MOHAMED MOWAFY
Short Hand If
If you have only one statement to execute you can put it on
the same line as the if statement.

Ex
a=2
b = 330
if a > b: print("a is greater than b")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


22
MOHAMED MOWAFY
Short Hand If Else
Ex

a=2
b = 330
print("A") if a > b else print("B")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


23
MOHAMED MOWAFY
and logical operator
logical operators used to combine conditional statements.
returns true if all conditions are true.
Ex
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
COMPUTER SYSTEMS PROGRAMMING (CCE 343)
24
MOHAMED MOWAFY
or logical operator
logical operators used to combine conditional statements.
returns true if one of the conditions are true.
Ex
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
COMPUTER SYSTEMS PROGRAMMING (CCE 343)
25
MOHAMED MOWAFY
not logical operator
The not keyword is a logical operator used to reverse the
result of the conditional statement.
returns true if the condition is false
Ex
a = 33
b = 200
if not a > b:
print("a is NOT greater than b")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


26
MOHAMED MOWAFY
Exercises

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


MOHAMED MOWAFY
27
Exercise (1)
Write Python program to Calculate the area of a rectangle.

length = float(input("Enter the length of the rectangle: "))


width = float(input("Enter the width of the rectangle: "))
area = length * width
print("The area of the rectangle is : ", area)

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


28
MOHAMED MOWAFY
Exercise (2)
Write Python program to check if a number is positive, negative,
or zero.

number = int(input("Enter a number: "))


if number > 0:
print("The number", number, "is positive.")
elif number < 0:
print("The number", number, "is negative.")
else:
print("The number", number, "is zero.")
COMPUTER SYSTEMS PROGRAMMING (CCE 343)
29
MOHAMED MOWAFY
Exercise (3)
Write Python program to check if a number is a given number is
even or odd.

number = int(input("Enter a number: "))


if number % 2 == 0:
print("The number", number, "is even.")
else:
print("The number", number, "is odd.")

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


30
MOHAMED MOWAFY
Questions

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


MOHAMED MOWAFY
31
Question
To specify the data type of a variable nam to string we use

a) string nam="Ahmed"
print(nam)

b) str nam="Ahmed"
print(nam)
c) nam=string("Ahmed")
print(nam)

d) nam=str("Ahmed")
print(nam)

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


32
MOHAMED MOWAFY
Question
What is the output of the following Python code?
a = 200
b = 33
c = 500
if a > b and c > a:
print("A B")

a) A
b) B
c) AB
d) BA

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


33
MOHAMED MOWAFY
Question
What is the output of the following Python code?

a=2
b = 330
print("A") if a > b else print("B")

a) A
b) B
c) AB
d) BA

COMPUTER SYSTEMS PROGRAMMING (CCE 343)


34
MOHAMED MOWAFY

You might also like