0% found this document useful (0 votes)
5 views7 pages

Python Programming Conditional Tasks

The document outlines a practical assignment for first-year B.Tech/MBA(Tech) students at SVKM’s NMIMS University, focusing on Python programming with conditional statements and strings. Students are required to complete various tasks, including triangle classification, a function definition, a BMI calculator, and string manipulations. The assignment emphasizes the application of conditional statements and string operations, with specific instructions for submission format and documentation.

Uploaded by

ekashastri1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Python Programming Conditional Tasks

The document outlines a practical assignment for first-year B.Tech/MBA(Tech) students at SVKM’s NMIMS University, focusing on Python programming with conditional statements and strings. Students are required to complete various tasks, including triangle classification, a function definition, a BMI calculator, and string manipulations. The assignment emphasizes the application of conditional statements and string operations, with specific instructions for submission format and documentation.

Uploaded by

ekashastri1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering


Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

PRACTICAL 2
Part A (To be referred by students)

Conditional Statements & Strings


SAVE THE FILE AND UPLOAD AS (RollNo_Name_Exp2)

Problem Statement: Write Python program to


1. Accept three sides of a triangle and print if the triangle is equilateral, isosceles or
scalene.
2. A function f is defined as follows :
f(x) = ax3 – bx2 + cx –d, if x > k
= 0, if x = k
= -ax3 + bx2 – cx +d, if x < k
Write a program that reads a, b, c, d, k and x and prints the value of f(x).
3. Implement Body Mass Index calculator. Define BMI function to accept weight and
height as parameter and displays appropriate BMI status.
BMI = weight(kg)/Height2
BM Status
<= 18.4 Underweight
18.5 – 24.9 Normal
25.0 – 39.9 Overweight
>=40 Obese
4. Implement distance convertor to convert distances by reading choice from user
a. Inches to feet
b. Cm to meter
5. Write a program to read and display a string
6. Define one string and display all elements at odd indexes using slicing operator
7. Write a program to check entered string is palindrome or not.

Topic covered: Conditional Statements (if, if…else, nested if, elif, pass and Strings, ),

Learning Objective: Learner would be able to


1. Analyze the scenario to write script applying conditional statements.
2. Infer the features of conditions for true and false decision making.
3. Solve the problems using decision making statements
4. Process string and perform various slicing operations on strig
Theory:
 The if statement:

1|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

Syntax of if statement

if test_expression:
body of if
The program evaluates the test expression and will
execute statement(s) only if the test expression
is True.
If the test expression is False, the statement(s) is
not executed.
In Python, the body of the if statement is indicated
by the indentation. The body starts with an
indentation and the first unindented line marks the
end.
Python interprets non-zero values
as True. None and 0 are interpreted as False.
 The if…else statement:
Syntax of if…else statement

if test_expression:
body of if
The if..else statement evaluates test
expression and will execute the body
of if only when the test condition is True.
If the condition is False, the body of else is
executed. Indentation is used to separate the
blocks.

 The if…elif…else statement:


The if…elif…else statement is used for decision making.

2|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

Syntax of for Loop

if test_expression:
body of if
elif test_expression:
body of elif
else:
body of else
The elif is short for else if.
It allows us to check for
multiple expressions.
If the condition
for if is False, it checks
the condition of the
next elif block and so on.
If all the conditions
are False, the body of else
is executed.
Only one block among the
several if...elif...else block
s is executed according to
the condition.
The if block can have only
one else block. But it can
have multiple elif blocks.

# Check if number is positive or negative or zero:


num = int(input("Enter a number "))
if num > 0:
print("Positive number")
elif num == 0:

3|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

print("Zero")
else:
print("Negative number")
Output:
Enter a number 2
Positive number

 The Nested if statement:


We can have a if...elif...else statement inside another if...elif...else statement. This is
called nesting in computer programming.
Any number of these statements can be nested inside one another. Indentation is the only
way to figure out the level of nesting.

# Check if number is positive or negative or zero:


num = int(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Output:
Enter a number: 0
Zero

String
• String literals in python are surrounded by either single quotation marks, or double
quotation marks.
• 'hello' is the same as "hello".
• You can display a string literal with the print( ) function:
• Example
print("Hello")
print('Hello')

4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

• Assigning a string to a variable is done with the variable name followed by an equal
sign and the string:
• Example
a = "Hello"
print(a)
### Str --- String ###
name=u'\u092a\u0948\u0925\u0964\u0928'
print(name)

# Slicing operations on string [start:stop:stepsize], if start and stop is not specified it


takes 0 and n-1 and if step size in not specified it is 1.

# slicing the string stringname[start:stop:stepsize]


str='core python'
print(str[0:9:1])

#str[0:9:2] --> stepsize 2


#str[2:4:1] --> 2 to 3 in size 1
#str[ : :2] --> access entire string in steps of 2.
#str[ :4: ] --> access string from 0 to 3 and stepsize is 1.
#str[-4:-1: ]--> access string from -4 to -2 from left to right
#str[-1:-4:-1]-> -1 to -3 from right to left...
#str[-1::-1] --> retrieves from last to first element [reverse]

#Repeating String...
print(4*"Python")

Concatenation (Joining)
s1="Core"
s2="Python"
s=s1+s2
print(s)

#Checking Membership
Write a program to find sub string in main string...
#operator:- in -- if found returns true ---And--- not in-- returns false if not found
str=input('Enter String')
sub=input('Enter sub string')
if sub in str:
print("Found")
else:
print("Not Found")
5|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

# Comparing String...
s1='Box'
s2="box"
if(s1==s2):
print("Same")
else:
print("Not Same")

6|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2023-2024 Semester: II

PRACTICAL 2
Part B (to be completed by students)

Conditional Statements and Strigs


1. All the students are required to perform the given tasks in Jupyter Notebook
2. Create a new notebook for each experiment. The filename should be
RollNo_Name_Exp1)
3. In the first cell, the student must write his/her Name, roll no and class in the form of
comments
4. Every program should be written in separate cells and in the given sequence
5. After completing the experiment, download the notebook in pdf format. The filename
should be RollNo_Name_Exp1.pdf).
6. Upload the pdf on the web portal

7|Page

You might also like