TASK - 1
Five students enter the classroom one after another. The first student gets roll number 1, the second gets 2, and so on. Display the assigned roll numbers.
Using for loop
for i in range(1,6,1):
print("student",i,"roll no:",i)
Output:
student 1 roll no: 1
student 2 roll no: 2
student 3 roll no: 3
student 4 roll no: 4
student 5 roll no: 5
Using while loop
Students = 1
while Students<=5:
print("student",Students,"roll no",Students)
Students = Students + 1
Output:
student 1 roll no 1
student 2 roll no 2
student 3 roll no 3
student 4 roll no 4
student 5 roll no 5
TASK - 2
A coach asks students to stand only at even-numbered positions in a queue. Display the first five positions.
Using for loop
for i in range(2,11,2):
print("student position no:",i)
Output:
student position no: 2
student position no: 4
student position no: 6
student position no: 8
student position no: 10
Using while loop
i = 1
even_position = int(input ("enter a number:"))
while(i<=even_position):
if(i % 2 == 0):
print("student position no",i)
i = i + 1
Output:
enter a number:10
student position no 2
student position no 4
student position no 6
student position no 8
student position no 10
TASK - 3
A newly constructed building has a special lift that does not stop on every floor. For safety reasons, it stops only at every third floor. A person enters the lift at the ground floor and presses the "Up" button.Display the first five floors where the lift will stop.
Using for loop
for i in range(3,16,3):
print("floor no:",i)
Output:
floor no: 3
floor no: 6
floor no: 9
floor no: 12
floor no: 15
Using while loop
i = 3
while(i<=15):
print("floor no:",i)
i = i + 3
Output:
floor no: 3
floor no: 6
floor no: 9
floor no: 12
floor no: 15
TASK - 4
A rocket launches with a countdown using only even numbers from 10. Display the countdown.
Using for loop
for i in range(10,-1,-2):
print("countdown:",i)
Output:
countdown: 10
countdown: 8
countdown: 6
countdown: 4
countdown: 2
countdown: 0
Using while loop
i = 10
while(i>=0):
print("countdown:",i)
i = i - 2
Output:
countdown: 10
countdown: 8
countdown: 6
countdown: 4
countdown: 2
countdown: 0
Pre-defined modules:
- math -Mathematical functions (e.g.,sqrt(),factorial(), sin())
- random - Generate random numbers and make random selections
- datetime - Work with dates and times
- time - Time-related functions (delays, timestamps)
- os - Interact with the operating system (files, directories,environment variables)
- sys - Access system-specific parameters and functions
- statistics - Statistical calculations (mean, median, mode, etc.)
- string - String constants and helper functions
- re - Regular expressions (pattern matching)
- json - Read and write JSON data
- csv - Read and write CSV files
- collections - Specialized container data types like Counter, deque, and defaultdict
- itertools - Efficient looping and iterator tools
- functools - Higher-order functions and decorators
- pathlib - Object-oriented file and directory path handling
Pre-defined functions:
- print(): Outputs text or data to the console.
- input(): Reads a line of text entered by the user.
- int(): Converts a value to an integer.
- float(): Converts a value to a floating-point number.
- str(): Converts a value to a string.
- list(): Converts an iterable into a list.
- dict(): Creates a new dictionary.
- tuple(): Converts an iterable into a tuple.
- set(): Converts an iterable into a set.
- len(): Returns the number of items in an object.
- type(): Returns the data type of an object.
Type casting:
Type casting in Python is the process of converting a variable from one data type to another using built-in constructor functions.
Example:
age = input("enter:")
result = int(age)
print(result)
Output:
enter:22
22
Programming Paradigm:
Python is a multi-paradigm programming language, meaning it supports multiple programming styles rather than forcing you to use just one.
Supported Programming Paradigms:
- Object-Oriented Programming (OOP)
- Procedural Programming
- functional Programming
- Imperative Programming
Top comments (0)