Lab 1 - Introduction To Python
Lab 1 - Introduction To Python
Intelligence
Spring 2023
Lab 1
INSTRUCTOR: DR. DIAA SALAMA ABDEL MONEIM
ENG. NOUR ELHUDA ASHRAF ,ENG. VERINA ADEL, ENG. TAREK TALAAT
Getting started with python:
⮚ Why do people use python?
First, we must admit that the choice of development tools is sometimes based on
unique constraints or personal preference. But there are some primary
factors in which python excels, such as:
● Developer productivity:
● Program portability:
Most Python programs run unchanged on all major computer platforms. Porting
Python code between Linux and Windows, for example, is usually just a matter
of copying a script’s code between machines.
● Support libraries:
Python comes with a large collection of prebuilt and portable functionality, known
as the standard library. This library supports an array of application-level
programming tasks, from text pattern matching to network scripting.
Python can be extended with both homegrown libraries and a vast collection of
third-party application support software. Python’s third-party domain offers
tools for website construction, numeric programming, serial port access, game
development, and much more.
The NumPy extension, for instance, has been described as a free and more
powerful equivalent to the MATLAB numeric programming system.
● Component integration:
Python scripts can easily communicate with other parts of an application, using a
variety of integration mechanisms.
Python code can invoke C and C++ libraries, can be called from C and C++
programs, can integrate with Java and .NET components and can interact over
networks.
PAGE 1
● Enjoyment:
Because of Python’s ease of use and built-in toolset, it can make the act of
programming more pleasure than chore.
Comments in Python can be done in two manners: single line comment is done by
beginning the statement with hash mark (#) or block comment which is done
by placing a block of code/statements in between of three successive single
quotes (‘’’) (or double quotes).
In Python, data takes the form of objects, either built-in objects that Python provides, or
objects we create using Python classes.
Objects are essentially just pieces of memory, with values and sets of associated
operations. (Everything is an object in a Python script)
There are no type declarations in Python, the syntax of the expressions you run determines
the types of objects you create and use.
PAGE 2
Now let’s dive through some of these built- Now let’s dive through some of these
built-in data types
PAGE 3
2. Textual data types (strings)
Strings are used to record both textual information (your name, for instance) as
well as arbitrary collections of bytes (such as an image file’s contents). They are
our first example of what in Python we call a sequence which is a positionally
ordered collection of other objects.
Strings are sequences of one-character strings; other, more general sequence types
include lists and tuples.
In Python, indexes are coded as offsets from the front, and so start from 0, we can
also index backward (from the end), positive indexes count from the left, and
negative indexes count back from the right.
In a slice, the left bound defaults to zero, and the right bound defaults to the
length of the sequence being sliced. (HINT: Extended slicing)
PAGE 4
Python also supports repetition in a simple way
Every string operation is defined to produce a new string as its result, because
strings are immutable in Python, they cannot be changed in place after they are
created.
you can never overwrite the values of immutable objects. For example, you can’t
change a string by assigning to one of its positions, but you can always build a
new one and assign it to the same name.
PAGE 5
3. Boolean data type
As in all programming languages, Python supports Boolean variables.
But keep in mind two things you MUST assign variable with True/False not 1/0 or it will be
considered storing integer values. Another thing is true and false are Variable Naming
Restrictions
4. Variable Naming Restrictions
1. Variable names in python can be any length.
PAGE 6
⮚ Type Casting
2. Bitwise Operations
Operator Description Statement Expected output
& ADD 2&3 2
| OR 2|3 3
~ NOT ~2 1
^ XOR 1^2 3
>> N bits Right Shift 40 >> 3 5
<< N bits Left Shift 3 << 2 12
PAGE 7
3. Comparison Operations
Operator Description
== Equal
!= Not Equal
< Less than
> Greater than
<= Less than or Equal
4. Logical Operations
Operator Statement Expected output
and 2 < 3 and 3 > 2 True
or 2 < 3 or 3 < 2 True
not not (2 < 3) False
5. Membership Operations
Operator Statement Expected output
in ‘H’ in ‘Haytham’ True
not in ‘M’ not in ‘Haytham’ True
6. Identity Operations
Operator Statement Expected output
is 2 is 2 True
is not ‘M’ is not ‘M’ False
PAGE 8
Python statements and syntax:
⮚ Control flow
Python statements used for controlling the flow of your program are:
● If … Else
● If … Elif … Else
2. For Statement
PAGE 9
As in almost all languages for loop in Python has iterator and provided number of loops
but has a different syntax surely.
In Python, iterator is not limited to integer values. It can be any object such as character,
string, or even user-defined object. Another merit, you do not need to declare it.
3. While Statement
While Loop does not differ in Python from other languages except you may see else
associated with a while loop and it is not an error.
PAGE 10
Exercises:
1. Write a program to prompt the user for hours
and rate per hour to compute gross pay.
Consider that the factory gives the employee
1.5 times the hourly rate for hours worked
above 40 hours.
Expected Input/Output script:
Enter the total worked hours:
57
Enter the rate per hour:
2
The gross pay is 131
PAGE 11