Python Assignment
Python Assignment
Program :-
print("Program to demonstrate variable declaration:")
x = 10 # x is variable here
print("The value of Int variable is as follows:")
print(x)
y = "Ankit" # y is variable here
print("The value of string or char variable is as follows: ")
print(y)
OUTPUT:-
ii) Demonstrate literals in python
Program:-
# String literal
t = "Hi!\
How are you?" # Multiline literal
print (t)
OUTPUT:-
# Numeric literal
a = 23 # Interger literal
b = 45
c = 34.62 #Float literal
print (a)
print (b)
print (c)
OUTPUT:-
iii)Demonstrate format in python
Program:-
# Format in python
name = "John"
age = 25
print("My name is {} and I'm {} years old.".format(name, age))
OUTPUT:-
#named indexes:
txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
#numbered indexes:
txt2 = "My name is {0}, I'm {1}".format("John",36)
#empty placeholders:
txt3 = "My name is {}, I'm {}".format("John",36)
print(txt1)
print(txt2)
print(txt3)
OUTPUT:-
IV)Demonstrate Keyword in python
Python keywords are reserved words that have a special meaning associated with them and can’t be used for anything but those
specific purposes. Each keyword is designed to achieve specific functionality.
x = 10
y = 20
# or condition
# at least 1 need to be true to execute if block
# or is keyword here
if x > 5 or y < 100:
print(x + 5)
# not condition
# condition must be false
# not is keyword here
if not x:
print(x + 5)
OUTPUT:-
V)Demonstrate Control characters in python
In Python, control characters are special characters that are used to represent non-printable characters, such as
newline, tab, and backspace
Program:-
# Newline character
# Tab character
# Backspace character
# Carriage return
print("Hello\nWorld!")
print("Name:\tAnkit\nAge:\t25")
print("Manchester\bUnited!")
print("Good\rBye!")
OUTPUT:-
VI)Demonstrate Implicit and Explicit line joining in python
Implicit line joining is when we split a logical line into multiple physical lines without having to use the backslash
character at the end of each physical line
Program:-
print(list)
Output:-
The backslash character acts as a “continuation” character telling Python that the current line is continued on the next
line. This is also called “explicit line joining”.
Program:-
text= "This text is one logical line to python/ but is 5 physical lines to us humans/when we put a backsalsh character/at
the end of each physical line/except the last physical line." # Explicit line joining
print(text)
Output:-
2 ) Objective :- To demonstrate various types of operators used in python
programming
# Arithmetic Operators
a = 10
b=3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus
print(a ** b) # Exponentiation
OUTPUT
# Relational Operators
a = 10
b=3
print(a == b) # Equal
OUTPUT
#Logical operators
Program:-
a=5
b = 10
ret = ( (a <= b) or (a != b) )
# 5 <= 10 ==> true. 5 != 10 ==> true. So, true(1) or true(1) will return true.
# 5 < 10 ==>true. 5 == 10 ==> false. So, true(1) and false(0) will return false.
OUTPUT
Boolean Opertors
Program:
x = True
y = False
print(x and y)
# Boolean or operator
x = True
y = False
print(x or y)
print(x or True)
print(y or False)
x = True
y = False
print(not x)
print(not y)
print(not True)
3) Objective: To demonstrate the precedence and associativty in python program by showing
the precedance chart
i) Precedence
Program:-
"""
The following chart shows the precedence and associativity of operators in Python, from highest to lowest:
-------------------------------------------
1 () Parentheses (grouping)
2 ** Exponentiation
9 or Logical OR
"""
a=5
b = 10
c = 15
# demonstrate precedence
ii) Associativty
Program:-
a=1
b=3
c=6
# demonstrate associativity
Output:-
4) Objective:- To demonstrate Sequential control structure in python
programming
# program to calculate the average of three numbers
num1 = 10
num2 = 20
num3 = 30
average = sum / 3
OUTPUT
5) Objective:- To demonstrate Selection control structure in python
programming
1) IF STATEMENT
# IF statement
x = int(input("Enter x: "))
if x > 0:
print(f"x is {x}")
OUTPUT:-
2) IF-ELSE STATEMENT
# program to check if a number is positive or negative
num = -5
if num >= 0:
print(num, "is positive")
else:
print(num, "is negative")
OUTPUT:-
3) NESTED IF STATEMENT
num = 0
if num > 0:
print(num, "is positive")
else:
if num < 0:
print(num, "is negative")
else:
print(num, "is zero")
OUTPUT:-
4) ELIF STATEMENT
marks = 75
OUTPUT:-
6) Objective:- To demonstrate Iteration control structure in python programming
i)For loop
for i in range(25,29):
print(i)
OUTPUT:-
for x in mylist:
print(x)
OUTPUT:-
II) WHILE LOOP
Program:-
n=1
sum = 0
while n <= 5:
sum += n
n += 1
OUTPUT:-
7) Objective :-To demonstrate loop control (break,continue) statement in
python
Program:-
if fruit == "cherry":
break
print(fruit)
if i % 2 == 0:
continue
print(i)
Ouput:-