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

Python Fundamentals

The document covers Python fundamentals, including data types such as integers, floats, booleans, strings, tuples, lists, sets, and dictionaries. It explains the concepts of mutability and immutability, variable assignment, and various operations on data types. Additionally, it introduces control flow structures like loops and f-strings for dynamic string creation.

Uploaded by

hassanali2415
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 views10 pages

Python Fundamentals

The document covers Python fundamentals, including data types such as integers, floats, booleans, strings, tuples, lists, sets, and dictionaries. It explains the concepts of mutability and immutability, variable assignment, and various operations on data types. Additionally, it introduces control flow structures like loops and f-strings for dynamic string creation.

Uploaded by

hassanali2415
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/ 10

Python Fundamentals

Python Fundamentals

1. Python Language Fundamentals

Python is an object-oriented language Every piece of data in the program is an Object.


Reference = symbol in a program that refers to a particular object. A single Python object can
have multiple references (alias). In Python Variable = reference to an object. When you assign an
object to a variable it becomes a reference to that object

Defining a variable:
❖ No need to specify its data type
❖ Just assign a value to a new variable name e.g. a =3

Page 2
Python Fundamentals

2.1 Data Types


o Basic data types
▪ int, float, bool, str
▪ None
▪ All of these objects are immutable
o Composite data types
▪ tuple immutable list of objects)
▪ list, set, dict (mutable collections of objects)
An object that can be changed after creation is referred to as mutable in Python. Immutable, on
the other hand, in Python refers to an object that cannot be changed after it is created.
Immutable ensures integrity (avoiding erroneous editing etc.) while mutable gives flexibility.

2.2 int, float

❖ Available operations
+, -, *, /, // (integer division), % remainder, ** square root
Example:
x=9
y=5
r3 = x / y = 1.8
r4 = x ** 2 = 81
r5 = x – y =4
r6 = x + y = 14
r7 = x * y = 45
r1 = x // y = 1
r2 = x % y = 4

2.3 bool
❖ Can assume the values True, False
❖ Boolean operators: and, or, not

Example:
is_sunny = True
is_hot = False
is_rainy = not is_sunny # is_rainy = False
bad_weather = not(is_sunny or is_hot) # bad_weather =False

Since is_sunny is True, is_sunny or is_hot is True, so not (is_sunny


or is_hot) is False, so bad_weather = False

temperature1 = 30
temperature2 = 35
growing = temperature2 >temperature1 # growing = True

Page 3
Python Fundamentals

2.4 String
string1 = "Python's nice"
# with double quotes

string2 = 'He said " yes"'


# with single quotes

print(string1)
print(string2) Note: Definition with single or double quotes is equivalent

2.5 Conversion between types

2.6 Strings: Concatenation

Page 4
Python Fundamentals

2.7 Sub-strings
❖ str[start:stop]
o The start index is included, while stop index is excluded
o Index of characters starts from 0

• x = 'This is python class'


print(x[0])
print(x[1])
print(x[0:2])
# Negative Index also works which is cool

• Negative indices: count characters from the end


• -1 = last character
print(x[-4:-2])
• fname = 'Faizan'
sname = 'Irshad'
print(fname + sname)
print(fname *3)

print('Faizan' in fname)

Note: Strings are immutable


str1 = "example"
str1[0] = "E"# will cause an error

Page 5
Python Fundamentals

2.8 None type


• Specifies that a reference does not contain data.
• def add_numbers(x,y,z=None):
if(z == None):
return x+y
else:
return x+y+z

print(add_numbers(1,2,5)) = 8

print(add_numbers(1,2)) = 3

Useful to:
• Represent "missing data" in a list or a table
• Initialize an empty variable that will be assigned later on

2.9 Tuple
I. Immutable list of variables
o x = (1,'a',2,'b') #Tuple class
o print(type(x))

II. Assigning a tuple to a set of variables


t1 = (2, 4, 6)
a, b, c = t1
print(a) #2
print(b) # 4
print(c) # 6

III. Swapping elements with tuples


a=1
b=2
a, b = b, a
print(a)
print(b)
IV. Accessing elements of a tuple
t [start:stop]

Page 6
Python Fundamentals

2.10 List
Mutable sequence of heterogeneous elements
Each element is a reference to a Python object
x = [1,'a',2,'b'] //List Class
print(type(x))
Adding elements and concatenating lists
l1 = [2, 4, 6]
l2 = [10, 12]
l1.append(8) # append an element to l1
l3 = l1 + l2 # concatenate 2 lists
print(l1)
print(l3)
Other Methods:
l1.count(element):
Number of occurrences of element
l1.extend(l2):
Extend list1 with another list l2
l1.insert(index, element):
Insert element at position
l1.pop(index):
Remove element by position

Note: Iterate over list elements

For Loop

x = [1, 2, 3]
for item in x:
print(item)

output is
1
2
3

Page 7
Python Fundamentals

While Loop

i=0
while (i != len(x)):
print(x[i])
i = i+1

output is
1
2
3

i=0
while (i < 5):
print([i])
i = i+1

output is
[0]
[1]
[2]
[3]
[4]

Another for loop


for x in range(10):
if x == 3:
continue # go immediately to the next iteration
if x == 5:
break # quit the loop entirely
print(x)

2. Set

Unordered collection of unique elements.

Operators between two sets


| (union), & (intersection), - (difference)

Page 8
Python Fundamentals

Add/remove elements

S1.discard(3) # to drop element

Set can be used to remove duplicates from list


item_list = [1, 2, 3, 1, 2, 3]
num_items = len(item_list) # 6
item_set = set(item_list) # {1, 2, 3}
print(item_set)
3. Dictionary

Collection of key value pairs


Allows fast access of elements by key
Keys are unique:
x={'cma':'[email protected]','ICAP':'[email protected]’}
print(x['cma'])

x={'cma':'[email protected]','ICAP':'[email protected]’}
for name, email in x.items():
print(name)
print(email)
4.1 Access by Key:

4.2 Reading keys and values

4.3 Adding updating values

4.4 Deleting a Key:

Page 9
Python Fundamentals

4.5 Iterating keys and values

This is a for loop that iterates over the items of the dictionary (d1). In each iteration, it adds the cost (v) of
the current item to the cumulative cost (cost) and then prints the item name and the updated cumulative cost.

An f-string, or "formatted string literal," is a way to embed expressions inside string literals in Python.
It allows you to include variables and expressions directly within string literals, making it easier to
create strings with dynamic content. F-strings are introduced by placing an 'f' or 'F' prefix before the
string and then enclosing expressions within curly braces {}.

name = "John"
age = 25

# Using f-string to create a dynamic string


sentence = f"My name is {name} and I am {age} years old."

print(sentence)
output: My name is John and I am 25 years old.

Page 10

You might also like