0% found this document useful (0 votes)
5 views37 pages

Intro To Python

The document provides an overview of Python programming fundamentals, covering its definition, capabilities, and syntax. It discusses data types, variables, functions, and the importance of indentation in Python code. Additionally, it includes examples and exercises to reinforce learning concepts.

Uploaded by

azanakhann020
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)
5 views37 pages

Intro To Python

The document provides an overview of Python programming fundamentals, covering its definition, capabilities, and syntax. It discusses data types, variables, functions, and the importance of indentation in Python code. Additionally, it includes examples and exercises to reinforce learning concepts.

Uploaded by

azanakhann020
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

Python – Fundamentals

Mr. M Hamza

Mr. M Hamza Spring 2025 1


(muhammadhamza@[Link])
• What is Python?
• What python can do?
• Why python?
• Printing Output on the screen
• Data Types, Expression and Variables
• String

Mr. M Hamza Spring 2025 1


(muhammadhamza@[Link])
• List, Tuples, Dictionaries and Sets
• Conditions and Branching
• Loops
• Functions
• Classes
• Objects

Mr. M Hamza Spring 2025 1


(muhammadhamza@[Link])
What is Python?

Python is a popular programming language. It was created by Guido van


Rossum, and released in 1991.
It is used for:
❖ Web development (server-side),
❖ Software development,
❖ Mathematics,
❖ System scripting.
❖ AI
❖ And so on …
Mr. M Hamza Spring 2025 3
(muhammadhamza@[Link])
What Python can do?

❖ Python can be used on a server to create web applications.


❖ Python can be used alongside software to create workflows.
❖ Python can connect to database systems. It can also read and modify
files.
❖ Python can be used to handle big data and perform complex
mathematics.
❖ Python can be used for rapid prototyping, or for production-ready
software development.
Mr. M Hamza Spring 2025 3
(muhammadhamza@[Link])
What Python can do?

❖ 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 can be treated in a procedural way, an object-oriented way or a
functional way.

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
Python VS Other Programming Languages Syntax

❖ Python was designed for readability, and has some similarities to the
English language with influence from mathematics.
❖ Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
❖ 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.

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
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.

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
Print Hello World on the screen

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
Print sum of two numbers on the screen

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
What will be the output?

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
Data Types, Comments
Expressions, Variables

Mr. M Hamza Spring 2025 2


(muhammadhamza@[Link])
Variable

❖ A variable is a location in computer memory where a value can be stored


for later use in a program.
❖ Note
❖ In Python,
❖ There is no command for declaring a variable.
❖ Variables are created when a value is assigned to it.

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
Variables Naming Conventions

A variable can have a short name (like x and y) or a more descriptive name
(age, userName, total_volume). Rules for Python variables are:
❖ A variable name must start with a letter or the underscore character
❖ A variable name cannot start with a number
❖ A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
❖ Variable names are case-sensitive (age, Age and AGE are three different
variables)
Mr. M Hamza Spring 2025 3
(muhammadhamza@[Link])
Legal Variable Names

myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
What will be the output?

2myvar = “Usama"
my-var = “Amir"
my var = “Ali"

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
What is Camel Case?

Each word, except the first, starts with a capital letter:

Example:
myVariableName = “Ali"

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
What is Pascal Case?

Each word starts with a capital letter:

Example:

MyVariableName = “Ali"

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
Variables Exercise

• my_variable = 1 • x= 10
• print(type(my_variable)) • Y = 26
• print(my_variable) • sum = x+ y
• My_variable = 100 • diff = x – y
• print(my_variable) • prod = x*y
• z1 = x / y
• z2 = x // y

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
Case-sensitivity

❖ Python is case-sensitive Example


a=4
A = "Sally"
#A will not overwrite a

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
Assigning One Value to Multiple Variables

Example: Output
?
x = y = z = "Ali"
print(x)
print(y)
print(z)

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
Assigning Many Values to Multiple Variables

Example: Output
?
x, y, z = “Ali", “Amir", “Khan"

print(x)
print(y)
print(z)

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
Print() Function

❖ Print() is used to print something on the screen.


Output
❖ In the print() function, we can output multiple Python is awsome
variables, separated by commas.

Example:
x = "Python is awesome"
print(x)

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
What will be the Output?

x = "Python" Output
y = "is" ?
z = “fantastic"
print(x, y, z)

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
What will be the Output?

x = "Python "
y = "is " Output
z = "awesome" ?
print(x + y + z)

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
What will be the Output?

x = 5
y = "John" Output
print(x + y) ?

Mr. M Hamza Spring 2025 7


(muhammadhamza@[Link])
Data Types

Data Type Example


Integer (int) 8
Float (float) 3.14
String (str) “Hello World!”

Mr. M Hamza Spring 2025 3


(muhammadhamza@[Link])
Data Types

Command Result
type(8) int
type(3.14) float
Type (“Hello World!”) str

Mr. M Hamza Spring 2025 4


(muhammadhamza@[Link])
Type Casting

Changing the type of a variable


Command Result
float(2) 2.0
int(3.14) 3
int(‘1’) 1
int(’A’) Not possible
str(1) ‘1’

Mr. M Hamza Spring 2025 5


(muhammadhamza@[Link])
Data Types

Data Type Example


Boolean (bool) True
Boolean (bool) False
type(False) bool

Command Result
int(True) 1
int(False) 0

Mr. M Hamza Spring 2025 6


(muhammadhamza@[Link])
Comments

❖ Comments are used to explain code.


❖ Comments can be used to make the code more readable.
❖ Comments can be used to prevent execution when testing
code.

Mr. M Hamza Spring 2025 8


(muhammadhamza@[Link])
Comments in Python

In python, comments are of two types.

❖Single line comments


❖Multi line comments

Mr. M Hamza Spring 2025 8


(muhammadhamza@[Link])
Single Line Comments in Python

Mr. M Hamza Spring 2025 8


(muhammadhamza@[Link])
Multi Line Comments in Python

Mr. M Hamza Spring 2025 8


(muhammadhamza@[Link])
What will be the Output?

Mr. M Hamza Spring 2025 8


(muhammadhamza@[Link])
Taking Input from user

name = input("Enter your name:")


print(“Your name is: " + name)

Mr. M Hamza Spring 2025 8


(muhammadhamza@[Link])
Practice

• Write a program that converts number of minutes to


number of hours.

Mr. M Hamza Spring 2025 8


(muhammadhamza@[Link])

You might also like