Name : Pankaj Verma Location : New Delhi date : 03/09/2024
1. Understanding Variables
(a) Define a variable in Python. Provide an example of how to create a variable that stores
your name.
Variable in Python - Variables are containers for storing data values.
In [11]: #Example
In [3]: Name = "Pankaj" #Name is the variable here and it storing string value "Pankaj"
(b) What is the difference between a variable and a constant? Can we have constants in
Python?
A constant is a data item whose value cannot change during the program’s execution.
In [12]: #Example
My_name_is_pankaj = "Pankaj_Verma"
A variable is a data item whose value can change during the program’s execution.
In [9]: Code_lang = "Python"
In [10]: Code_lang
Out[10]: 'Python'
Python does not contain any constants. Instead, Python provides us a Capitalized naming convention method. Any variable written
in the Upper case is considered as a Constant in Python.
2. Working with Different Data Types
(a). Create variables of the following types in Python:
In [28]: Num = 1122 #integer
In [29]: print(Num)
1122
In [16]: point = 4.565 #float
In [30]: print(point)
4.565
In [22]: Name = "Pankaj" #String
In [31]: print(Name)
Pankaj
In [32]: Boole = (10 > 4)
In [33]: print(Boole)
True
(b). Write a Python script to display the type of each variable you created
In [35]: print(f"The type of Num is: {type(Num)}")
print(f"The type of point is: {type(point)}")
print(f"The type of Name is: {type(Name)}")
print(f"The type of Boole is: {type(Boole)}")
The type of Num is: <class 'int'>
The type of point is: <class 'float'>
The type of Name is: <class 'str'>
The type of Boole is: <class 'bool'>
3. Arithmetic Operators
(a). Explain the following arithmetic operators with examples:
Addition (`+`) = Adds two values together
In [53]: #Example
Name = "Pankaj" + "1234"
print(Name)
Pankaj1234
Subtraction (-) = subtracts two operands
In [54]: #Example
subs = 56-9
print(subs)
47
Multiplication (``) = Multiplies two values
In [55]: #Example
Multiple = 2*"Pankaj"
print(Multiple)
PankajPankaj
Division (`/`) = Divides the left-hand operand by the right-hand operand. This operator always returns a floating-point result, even
if both operands are integers.
In [56]: #Example
Div = 454/688
print(Div)
0.6598837209302325
Floor Division (`//`) = Divides two numbers and returns the largest integer that is smaller or equal to the result
In [58]: #Example
Floor = 6//4
print(Floor)
1
Modulus (`%`) = returns the remainder when the first operand is divided by the second
In [59]: #Example
Mod = 34%6
print(Mod)
4
Exponentiation (``) = Returns first raised to power second
In [60]: #Example
Exp = 2**4
print(Exp)
16
(b). Write a Python script to calculate the area of a rectangle using variables length and
width with values 5 and 10, respectively. Use the multiplication operator.
In [63]: length = 5
width = 10
AOR = length*width #Area of rectangle
print(AOR)
50
4.
Comparison and Logical Operators
(a). Explain the following comparison operators with examples:
Equal to (`==`) = True if both operands are equal else false
In [4]: #Example
x = 6
y = 6
z = 8
p = 4
print(x==y)
print(z ==p)
True
False
Not equal to (`!=`) = True if operands are not equal else false
In [6]: #Example
x = 7
y = 4
print(x!=y)
True
Greater than (`>`) = True if the left operand is greater than the right else false
In [8]: #Example
x = 5
y = 3
print(x>y)
True
Less than (`<`) = True if the left operand is less than the right else false
In [9]: #Example
g = 34
f = 56
print(g<f)
True
Greater than or equal to (`>=`) = True if left operand is greater than or equal to the right else false
In [10]: #Example
x = 5
y = 89
print(x>=y)
False
Less than or equal to (`<=`) = True if left operand is less than or equal to the right else false
In [12]: #Example
x = 90
y = 100
print(x<=y)
True
(b). Using logical operators ( and , or , not ), write a Python script that checks if a number
is positive and even.
In [22]: num = int(input("Enter a number: "))
if num > 0 and num % 2 == 0:
print(f"{num} is positive and even.")
elif num > 0 and num % 2 != 0:
print(f"{num} is positive but not even.")
else:
print(f"{num} is not both positive and even.")
7 is positive but not even.
5. Type Casting in Python
(a). What is type casting? Explain the difference between implicit and explicit type casting
with examples.
Type casting is a way to convert data from one type to another. In implicit type casting, the conversion of one data type to another
happens automatically by Python without any user intervention. In explicit type casting, the conversion is done manually by the
programmer using Python’s built-in functions, such as int(), float(), str(), etc.
In [30]: # Implicit type casting
x = 5
y = 3.2
add = x + y
print(add)
type(add)
8.2
Out[30]: float
In [31]: # Explicit type casting
a = "10"
b = int(a)
print(b)
type(b)
10
Out[31]: int
(b). Write a Python script that:
In [34]: # Converts a float to an integer
a = 5.236
b = int(a)
print(b)
type(b)
5
Out[34]: int
In [35]: # Converts an integer to a string.
a = 699
b = str(a)
print(b)
type(b)
699
Out[35]: str
In [37]: # Converts a string to a float.
a = "5.562"
b = float(a)
print(b)
type(b)
5.562
Out[37]: float
6. Practical Exercise: Mini Calculator
Write a Python script that asks the user to input two numbers and then:
In [49]: n_1 = int(input("Enter a number :"))
n_2 = int(input("Enter a number :"))
add = n_1 + n_2 #Adds the two numbers
subs = n_2 - n_1 #Subtracts the second number from the first
mult = n_1 * n_2 #Multiplies the two numbers
div = n_1 / n_2 #Divides the first number by the second
sum = str(add) #Converts the sum of the numbers to a string and prints the type
print(add)
print(subs)
print(mult)
print(div)
print(sum)
type(sum)
55
-45
250
10.0
55
Out[49]: str