Day-1:
Python Core Basics
• Primitive: int, float, bool, str
• Collections: list, tuple, set, dict - Mutable vs Immutable
• Type casting
Control Flow
• if, elif, else
• nested conditions
• match-case (Python 3.10+)
• Loops
• for loop, while loop
• break, continue, pass - nested loops
Functions
• function definition
• arguments (*args, **kwargs) - lambda functions
• recursion basics
Python Core Datatypes:
Dataypes:
What is meant by the datatype?
Data type is used to measure the data.
We have two types of data types:
Numeric Datatypes
Data Structures
Please learn what are the rules of the variable declaration.
Numeric DataTypes:
Int
Float
Complex
Boolean
Int: int contains the positive numbers, zero, negative numbers
Eg: 1, 2, 900, -2, -90, 0, etc.,
Float: float contains the decimal points
Eg:12.3, 45.6, -654.9, etc.,
Complex: Which contains both real number and imaginary
Eg: 2+4j, -4+5j, etc.,
Boolean: Which contains True and False
Eg: True or False
Example:----
a = 10
print(a)
print(type(a))
b = 10.5
print(b)
print(type(b))
c = 3+4j
print(c)
print(type(c))
d = True
print(d)
print(type(d))
output:
10
<class 'int'>
10.5
<class 'float'>
(3+4j)
<class 'complex'>
True
<class 'bool'>
Data Structures:
List
Tuple
String
Dictionary
Set
Frozen-set
List:
1. List is ordered, sequenced and mutable datatype.
2. The signifance of the list is []
3. List can contain the heterogenous elements(nothing can contain combination
of the datatypes)
4. List allow the duplicate values.
5. we can able modify the elements as well by using variable_name[index] =
value
Ordered datatype:
In which order the data has been given in the same way the data will be
retrieved.
Sequenced DataType:
We can able to access the elements by using the index values(Python
supports both positive Indexing and negative indexing).
Mutable DataType:
We can able to change the internal reference of the object.
To know the methods of the list
1. we will be using the dir
2. From these we will be getting the
i. magical methods and (which starts with __ and ends with the __)
ii. normal methods
'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
We can add the elements in three ways in list
1. Append
2. Extend
3. Insert
Know the definitions and difference between those
Append:
Append is used to add the element at the end of the list, But by using append we can
able to add only one element or one list (it will be considering as a one element)
If you try to add more than one element it will throw the error called Type Error.
Type Error: means using the functionality in the wrong way.
Extend:
Extend is used to add the elements at the end of the list, But all the elements will be
considered as a separate elements.
Insert:
Insert is used to add the element at the specific position
insert(position, element)
if give the index in the positive which does not exist then it willl be adding the
element at the end of the list
if give the index in the negative which does not exist then it willl be adding the
element at the starting of the list
We can remove the elements in two ways in list
1. pop
2. Remove
Pop:
Pop is used to remove by using the index values
if it empty it is used to remove the last element
if the index value out of the list then it will give index error
Index Error: Index error based we don’t have that index in the list
Remove:
Remove is used to remove the element based on the values
If you have more than one element then it is used to delete or remove the first one
If the element is not there then we will get the ValueError
ValueError: Value Error because in our list we cannot able to find that particular
Value
Clear:
it is used to delete all the elements but the list
Reverse:
It is used to reverse the list of the elements
Sort:
Sorting is used to sort the elements if it is homogenous datatype
By default it will be ascending order
If you want you can give reverse = True
Count:
It is used to count the occurrences the elements
Index:
It is used to find the index of the element
If the element is not there then it will throw ValueError
Copy:
We have two types of copy
Reference copy
Normal copy
Reference copy id’s are same
o Ll = 1
Normal Copy id’s are different
o L11 = [Link]()
Tuple:
1. Tuple is ordered, sequenced and immutable datatype.
2. The signifance of the list is ,
3. Tuple can contain the heterogenous elements(nothing can contain
combination of the datatypes)
4. Tuple allow the duplicate values.
5. we cannot able modify the elements because it is immutable datatype.
Ordered datatype:
In which order the data has been given in the same way the data will be
retrieved.
Sequenced DataType:
We can able to access the elements by using the index values(Python
supports both positive Indexing and negative indexing).
Immutable DataType:
We cannot able to change the internal reference of the object.
we have only two methods
1. index
2. Count
Dictionary:
1. Dictionary is unordered, unsequenced and mutable datatype.
2. The signifance of the list is {}
3. dictionary can contain the heterogenous elements(nothing can contain
combination of the datatypes)
4. dictionary allow the duplicate values but not the duplicate keys.
5. we can able modify the values but not the keys.
6. keys should be immutable
7. values can be anything either mutable or immutable.
UnOrdered datatype:
In which order the data has been given in the same way the data will be not
be retrieved.
UnSequenced DataType:
We can able to retrieve the values by using the keys if we have duplicated
keys then we will be get the last key value.
Mutable DataType:
We can able to change the internal reference of the object.
Methods for the Dictionary:
'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault',
'update', 'values'
Clear : Clear is used to clear the dictionary keys and vales but not the
variable_name
copy: it is used to copy the keys and values in the new dictionary but cannot
able to change(so, we can say it is as normal copy not the reference e copy).
Get: it is used to retrieve the value if the key is present
if the key is not present then it will be none it don’t through any error.
Items: we can able to retrieve both keys and values in the list form but key and
value in the tuple form.
Keys: we can able to retrieve the keys in the list form
Values: we can able to retrieve the values in the list form
Update: it is used to add the key and vale pairs
Pop: pop must and should contain the key as an argument and it will return the
value
Popitem: popitem we cannot able to pass the argument it will return the last key
and value.
Fromkeys: it is used to create a new dictionary by default value will be None.
Or else you can add the valu which is you need it.
setdefault: setdefault means it will give the value if it exits it don’t replace if
the key is present.
if the key is not present it will add the existing dictionary and value will be
retrieved.
Type casting:
Converting from one datatype to another datatype
Shallow copy and deep copy:
shallow copy:
Import copy
[Link]() we will be calling it as a shallow copy
For nested mutable objects it will change the internal reference of the object is
known as the shallow copy.
Deep copy:
Import copy
[Link]() we will be calling it as a deepcopy
For mutable objects it will create the new internal reference of the object or we
can say it is as it mutable objects does not goes to the copied object is
known as the shallow copy.
Conditional Statements:
it is used to check whether the condition is true of false.
We have different types of conditional statements.
If
If-else
If-elif-else
Nested-if
Match
If:
it is used to check the condition is true or not
If it is true then it will print true
If the condition is false then output will be None
Syntax:
if condition:
Do something
If-else:
it is used to check the condition is true or not
If it is true then it will print if condition statement
If the condition is false then output will be else statement
Syntax:--
If condition:
Do something in A
Else:
Do something in B
If-elif-else:
When we have more than two statements then we can able to go for the
if-elif-else statement
First it will check with the if statement if the condition is true then it will
print the if condition
If it is false, it will go the elif statements it will check all the elif
statements and then it will print if the conditions are true
If all the elif statements false then it will be printing the else statement
Syntax:---
If condition:
Do A
Elif condition:
Do B
Elif condition:
Do C
Else:
Do D
Nested-if statement:--
If the condition is true, it will check one more if further that we can call it the nested-if
statement.
Or else it is same as the Elif statement
Synatx:---
If condition:
If condition:
Do something in a
Do something in b
Elif condition:
Do something in c
Elif condition:
Do something in d
Else:
Do something in e
Switch statement:
To check whether the pattern is matching or not, if it is not matching then it
will go the default statement that is denoted by the underscore “_”
Syntax:----
Match condition:
Case variable:
Print
Case variable:
Print
Case variable:
Print
Case variable:
Print
Case _:
Print
Loops:
It is used to iterate a loop until the condition false.
We have two types of loops
1. While loop
2. For loop
While loop:
Synatx:---
I=0
While i<4:
Print(i)
I +=1
I = 0 is the intilization
I < 4 is the condition
I+=1 is the increment or decrement
In while loop we don’t what is the end value, so it is risk.
For loop:
Synatx:---
Lst = [1, 2, 3, 4, 5]
For i in lst:
Print(i)
For i in range(start, end, step):
Print(i)
By using for loop, we know what the end value
Here initialization, condition and increment in the single line
Nested Loop:
A loop inside a another loop is known as the nested loop
Syntax:---
For i in (1, 2, 3):
for j in (1, 2):
print(i, j)
For-else loop:
If it executes the for loop, then it should not execute the else statement
If it executes the else statement, then it should not execute the for loop.
Syntax:
For loop:
If condition:
Print statement
Break
Else:
Print statement
Control statements:
It is used to control the functionality
We have three types
1. Break
2. Continue
3. Pass
Break:
It is used to break all the set of statements if the condition is true and ends
the loop right away
Break should be inside the loop
Syntax: ---
For i in (1, 2, 3):
If i ==2:
Break
Print(i)
Continue:
It is used to skip one loop cycle without stooping the loop.
Continue should be inside the loop
Not in condition statements or functions
Syntax: ---
For i in (1, 2, 3):
If i ==2:
continue
Print(i)
Pass:
When we think to implement in the future then we will be going for the
pass statement
Pass should be anywhere whether it is from the condition statements, loops,
functions, oops, etc.
Syntax:-----
If True:
pass
Function:
a function is a reusable block of code that performs a specific task.
You define it once and can call it multiple times, which helps avoid
repetition and makes programs easier to read and maintain.
Function is defined by the def keyword
Function name should be in the snake-case that means every character
should be lower case and it if has more than one word then it should
separate by using “_”
Syntax:---
def function_name(argument1, argument2):
return argument1+argument2
types of functions:
We have three types of functions
1. Mandatory argument
2. Default argument
3. Keyword arguments
4. Variable-length mandatory or default argument
mandatory function argument
Mandatory function argument means the number of parameters passing in the
function definition should be same in the function calling is known as the
mandatory function argument.
Example:---
def fun(a,b):
Return a+b
fun(1, 2)
fun(1) we will get TypeError because the functionality we are using is
wrong instead of two parameters, we are passing only one parameter
def fun(a,b)----> function definition
fun(1, 2) ------- > function calling
default argument
Default function argument means the number of parameters passing in the
function definition may be the same or may not be same as the function calling
is known as the mandatory function argument.
Example:---
def fun(a,b = 10):
Return a+b
fun(1, 2)
fun(1)
fun(1, 2, 3) we will get TypeError because the functionality we are
using is wrong instead of two parameters, we are passing only three
parameter
def fun(a,b)----> function definition
fun(1, 2) ------- > function calling
keyword arguments:
keyword argument means the number of parameters passing in the function
definition may be the same function calling but you can able to change the
position of the arguments is known as the keyword function argument.
def fun(a,b):
return a+b
fun(b=20, a=30)
Variable-length mandatory or default argument:
In keyword arguments we have *args and **kwargs
*args means data will be stored in the form of tuple
**kwargs means data will be stored in the form of dictionary
def keyword_arguments(*args, **kwargs):
print(args, type(args))
print(kwargs, type(kwargs))
Keyword_argumets(10, 20, 30, c=100, d=200)
function of over-riding
Python does not support the function overriding concept
Function over riding means function name must be same and parameters
will vary then the last declaration will come into consideration because it is
an interpreted language.
Here, interpreted language means line by line execution so the function
name will same name will vanish and stores which is there next it will be
saved
Syntax:----
def fuc(a,b):
print(a+b)
def func(a, b, c):
print(a+b+c)
recursion
A function that calls itself is known as recursion