DEV Community

Cover image for PYTHON TASKS
G Gokul
G Gokul

Posted on

PYTHON TASKS

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:
countdown: 10
countdown: 8
countdown: 6
countdown: 4
countdown: 2
countdown: 0

Pre-defined modules:

  1. math -Mathematical functions (e.g.,sqrt(),factorial(), sin())
  2. random - Generate random numbers and make random selections
  3. datetime - Work with dates and times
  4. time - Time-related functions (delays, timestamps)
  5. os - Interact with the operating system (files, directories,environment variables)
  6. sys - Access system-specific parameters and functions
  7. statistics - Statistical calculations (mean, median, mode, etc.)
  8. string - String constants and helper functions
  9. re - Regular expressions (pattern matching)
  10. json - Read and write JSON data
  11. csv - Read and write CSV files
  12. collections - Specialized container data types like Counter, deque, and defaultdict
  13. itertools - Efficient looping and iterator tools
  14. functools - Higher-order functions and decorators
  15. pathlib - Object-oriented file and directory path handling

Pre-defined functions:

  1. print(): Outputs text or data to the console.
  2. input(): Reads a line of text entered by the user.
  3. int(): Converts a value to an integer.
  4. float(): Converts a value to a floating-point number.
  5. str(): Converts a value to a string.
  6. list(): Converts an iterable into a list.
  7. dict(): Creates a new dictionary.
  8. tuple(): Converts an iterable into a tuple.
  9. set(): Converts an iterable into a set.
  10. len(): Returns the number of items in an object.
  11. 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)
Enter fullscreen mode Exit fullscreen mode

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:

  1. Object-Oriented Programming (OOP)
  2. Procedural Programming
  3. functional Programming
  4. Imperative Programming

Top comments (0)