Python Assignment 01
Python Assignment 01
Python Assignment 01
1. Understanding Variables
(a) Define a variable in Python. Provide an example of how to create a variable that stores
your name.
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 [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.
In [29]: print(Num)
1122
In [30]: print(point)
4.565
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
3. Arithmetic Operators
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.
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.
(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.
add = x + y
print(add)
type(add)
8.2
Out[30]: float
print(b)
type(b)
10
Out[31]: int
print(b)
type(b)
5
Out[34]: int
print(b)
type(b)
699
Out[35]: str
print(b)
type(b)
5.562
Out[37]: float
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 :"))
print(add)
print(subs)
print(mult)
print(div)
print(sum)
type(sum)
55
-45
250
10.0
55
Out[49]: str