You are on page 1of 21

1) Objective:- To Demonstrate variables, literals, format, keywords, control

charcters, implicit line joining, explicit line joining in python programming.

i) Demonstrate Variables in python

Syntax: Variable_name = value_to_store

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)

t = 'Good Morning!' # Singleline 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.

Python keywords are case-sensitive.

1. All keywords contain only letters (no special symbols)


2. Except for three keywords (True, False, None), all keywords have lower case letters

Program:- using operator keywords

x = 10
y = 20

# and to combine to conditions


# both need to be true to execute if block
# and is keyword here
if x > 5 and y < 25:
print(x + 5)

# 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:-

list= [1,2,3,4,5,6,7,8] # Implicit line joining

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

print(a != b) # Not equal to

print(a > b) # Greater than

print(a < b) # Less than

print(a >= b) # Greater than or equal to

print(a <= b) # Less than or equal to

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.

print("Return value of first expression is ", ret)

ret = ( ( a < b) and (a == b ) )

# 5 < 10 ==>true. 5 == 10 ==> false. So, true(1) and false(0) will return false.

print("Return value of second expression is ", ret)

ret = not ( ( a < b) and (a == b ) )

# we have used the same expression here.

# And its result was false.

# So not(false) will be true.

print("Return value of third expression is ", ret)

OUTPUT
Boolean Opertors

Program:

# Boolean and operator

x = True

y = False

print(x and y)

print(x and True)

print(y and False)

# Boolean or operator

x = True

y = False

print(x or y)

print(x or True)

print(y or False)

#Boolean not operator

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:-

# Precedence and associativity in Python

"""

The following chart shows the precedence and associativity of operators in Python, from highest to lowest:

Precedence Operator Description

-------------------------------------------

1 () Parentheses (grouping)

2 ** Exponentiation

3 +, - Unary plus and minus

4 *, /, % Multiplication, division, modulus

5 +, - Addition and subtraction

6 <, <=, >, >=, ==, != Comparison operators

7 not Logical NOT

8 and Logical AND

9 or Logical OR

"""

a=5

b = 10

c = 15

# demonstrate precedence

result1 = a + b * c # multiplication has higher precedence than addition

result2 = (a + b) * c # parentheses have highest precedence

print("Result 1:", result1)

print("Result 2:", result2)


Output:-

ii) Associativty

Program:-

a=1

b=3

c=6

# demonstrate associativity

result3 = a / b / c # division is left associative

result4 = a ** b ** c # exponentiation is right associative

print("Result 3:", result3)

print("Result 4:", result4)

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

# calculate the sum of the numbers

sum = num1 + num2 + num3

# calculate the average of the numbers

average = sum / 3

# print the average

print("The average of the numbers is:", average)

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

# program to check if a number is positive, negative or zero

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

# program to check the grade of a student

marks = 75

if marks >= 90:


print("Grade A")
elif marks >= 80:
print("Grade B")
elif marks >= 70:
print("Grade C")
elif marks >= 60:
print("Grade D")
else:
print("Grade F")

OUTPUT:-
6) Objective:- To demonstrate Iteration control structure in python programming

i)For loop

# For loop with range

for i in range(25,29):

print(i)

OUTPUT:-

# For loop with list

mylist = ['python', 'programming', 'examples', 'programs']

for x in mylist:

print(x)

OUTPUT:-
II) WHILE LOOP

Program:-

# Python program using while loop

# program to calculate the sum of first 5 natural numbers

n=1

sum = 0

while n <= 5:

sum += n

n += 1

print("The sum of first 5 natural numbers is", sum)

OUTPUT:-
7) Objective :-To demonstrate loop control (break,continue) statement in
python

Program:-

# Using break to exit loop

fruits = ["apple", "banana", "cherry", "orange"]

for fruit in fruits:

if fruit == "cherry":

break

print(fruit)

# Using continue to skip iteration

for i in range(1, 11):

if i % 2 == 0:

continue

print(i)

Ouput:-

You might also like