0% found this document useful (0 votes)
3 views

Lab 1 - Python

Python is a widely-used, open-source programming language created in 1991, known for its simplicity and versatility across various applications like Machine Learning and web development. It features a unique syntax that emphasizes readability and allows for multiple programming paradigms, including procedural and object-oriented programming. The document also covers key concepts such as variables, data types, operators, and user input in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 1 - Python

Python is a widely-used, open-source programming language created in 1991, known for its simplicity and versatility across various applications like Machine Learning and web development. It features a unique syntax that emphasizes readability and allows for multiple programming paradigms, including procedural and object-oriented programming. The document also covers key concepts such as variables, data types, operators, and user input in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

PYTHON

SECTION 1
Python
One of the world's most popular
programming languages.

Created by Guido van Rossum Released


in 1991

General-purpose language, it is used in


various areas of applications such as
Machine Learning, Artificial Intelligence,
web development, IoT, and more.
Why Python?
Open Source Works on different Can be treated in a
which means its platforms and An procedural way, an
available free of interpreter system object-oriented way
cost or a functional way

Simple syntax
Powerful Allows developers to
similar to the
development write programs with
English language
libraries include fewer lines than
and so easy to
AI, ML etc some other
learn
programming
languages.
Python's abilities

Create web applications Used to handle big data


and perform complex
mathematics

Connect to database Work with Data


systems. It can also Science, AI, and ML
read and modify files. technologies
Python vs other programming languages

Designed for readability, and has some


similarities to the English language with
influence from mathematics

Uses new lines to complete a command, as


opposed to other programming languages
which often use semicolons or parentheses.

Uses whitespace to specify loop, function,


and class scope. Curly-brackets are used in
other programming languages.
HOW TO DOWNLOAD AND INSTALL PYTHON

1- Download latest
version of python from:
“www.python.org”
Variables
What is variables?
• In Python, variables are like storage spaces where we can keep
information in our program.
• Computer's memory locations are having a number or address,
internally represented in binary form.
100
• It stores the values in a randomly chosen
108
memory location.
• Python's built-in id() function returns the 116
124
address where the object is stored.
132 A
140
148
156
Variables Declaration in python
• High level languages like Python make it possible to give a suitable
alias or a label to refer to the memory location.
• A Python variable is created automatically when you assign a value to
it.
• The equal sign (=) is used to assign values to variables.

Variable Name = Variable Value


Variables
Declaration in
Python
• In this example :
• age variable is an integer
variable
• name variable is a string
variable
• salary variable is a floating-
point variable
Python identifiers
• In Python, an identifier is a name given to entities like variables,
functions, classes, etc.
• Identifiers can be a combination of letters (in lowercase or uppercase),
digits (0-9), and underscore (_).
• They cannot start with a digit.
• Python is case-sensitive, meaning:
• myVar and myvar are different identifiers.

Valid identifiers Invalid identifiers


my_variable 1myvar
myVar my-var
My_variable_1 my var
Printing Python Variables
In python you can use the built-in function print()
to display output to the user.
1

4
Printing Python Variables
String Formatting (f-string):

Python 3.6 and later versions support f-strings, which provide a concise and
readable way to embed variables in strings.

Formatting with % operator:

In earlier Python versions, you can use the % operator for string formatting
Printing Python
Variables
Printing multiple lines:

You can use triple-quoted


strings ''' or """
to print text over multiple
lines.
Printing Python
Variables
Special characters:

You can include special


characters like newline \n or
tab \t in print statements.
Variables
Delete variables
Variables
Assign multiple variables
with the same value
Variables
Assign multiple variables with
different values
PYTHON COMMENT

Single line comment (#) Multiple line comment


“””
“””
Data Types
Data Types
Numeric
Data Types
string Data
Types
Data Types
Use ‘ type() ‘
To know data type of
variable

You can assign the


Same variable with
Different data type
Python Type Casting
It is the process of changing the data type of a variable from one type to another.

Python Type casting:

1. Implicit casting
• Python automatically converts one data type to another when needed.
2. Explicit casting
• You explicitly convert a data type to another using predefined functions like:
• Int()
• float()
• str()
Python Type Casting

• It is the process of changing


the data type of a variable
from one type to another.

• Python Type casting:


• Implicit casting
• Python automatically
converts one data
type to another when
needed.
• Example:
Python Type Casting

• It is the process of changing the


data type of a variable from one
type to another.

• Python Type casting:


• Explicit casting
• Example:
Operators
1- Python Arithmetic Operators
1- Python Arithmetic Operators
2- PYTHON COMPARISON OPERATORS
2- PYTHON COMPARISON
OPERATORS
3- Python Assignment Operators
3- Python Assignment Operators
USER INPUT
USER INPUT

◾ Input function enables the user to input data


◾ The program can not be executed until the user enter the inputs
◾ Use [ input(“”) ] function.

◾ input() → Data type of input data is String so if you want to change it use TYPE CASTING.
USER INPUT

Example: Write a program to read the user first name and last name then, print Hello to the
username.
USER INPUT
EX
◾ Write a program to read two number from user and calculate
summation.

solution
EX

◾ Write a program to read the user first ,middle and last name
then, print Hello to the username in the format like the
following:
“ Hello, Ahmed Mohamed Saad ”

Note: for starting each name with uppercase use [title()] or


[capitalize()] functions.
Solution

fName=input("What is your first name?")

mName=input("What is your middle name?")

lName=input("What is your last name?")

print(f"hello, {fName.strip().title()} {mName.strip().capitalize()} {lName.strip().capitalize()}")


THANK YOU!!

You might also like