DEV Community

Kiruthiga S
Kiruthiga S

Posted on

Python

Predefined functions

all()-Returns True if all items in an iterable object are true
any()-Returns True if any item in an iterable object is true
abs()-Returns the absolute value of a number
ascii()-Returns a readable version of an object. Replaces none-ascii characters with escape character
bin()-Returns the binary version of a number
bool()- Returns the boolean value of the specified object
bytearray()-Returns an array of bytes
float()-Returns a floating point number
input()-Allowing user input
len()-Returns the length of an object

Pre-defined Modules

operator-Functional interface to operators (add, mul, itemgetter, attrgetter)
array-Efficient arrays of basic numeric types (compact alternative to lists)
calendar-Work with dates as calendars; print text calendars and compute month info
cgi-Helpers for Common Gateway Interface (legacy web server scripts)
csv-Read and write CSV files (comma-separated values) with ease
decimal-Fixed-point and floating decimal arithmetic for money and exact math
io-Input/Output streams and buffering (text and binary)
JSON-Encode and decode JSON data (JavaScript Object Notation)
keyword-Test for Python keywords; list all keywords
token-Token constants used by the Python tokenizer

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

Type casting
The process of converting a variable from one data type to another using built-in constructor functions

age = input("Enter Your Age:")
result = int(age)
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:Enter Your Age:21
21

1) A teacher asks five students to stand in a line. Each student wears a badge with the number 1. Display the badge numbers from left to right.

student = 1
print(1, end = ' ') 
student = student + 1  #student = 2
print(1, end = ' ')
student = student + 1 #student = 3
print(1, end = ' ')
student = student + 1 #student = 4
print(1, end = ' ')
student = student + 1 #student = 5
print(1, end = ' ') 
student = student + 1 #student = 6
Enter fullscreen mode Exit fullscreen mode

Output:1 1 1 1 1

student = 1
while student <=5:
    student = student + 1
    print(1, end = ' ') 
Enter fullscreen mode Exit fullscreen mode

Output:1 1 1 1 1

2) 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.

student = 0
while student <5:
    student = student + 1
    print(student, end = ' ') 
Enter fullscreen mode Exit fullscreen mode

Output:1 2 3 4 5

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

3) A coach asks students to stand only at even-numbered positions in a queue. Display the first five positions.

for i in range(2,11,2):
    print('Position:',i)
Enter fullscreen mode Exit fullscreen mode

Output:Position: 2
Position: 4
Position: 6
Position: 8
Position: 10

count=1
position=0
while(count<6):
    position=position+2
    count=count+1
    print('Position:',position)
Enter fullscreen mode Exit fullscreen mode

Output:Position: 2
Position: 4
Position: 6
Position: 8
Position: 10

4) 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.

for floor in range(3, 16, 3):
    print(floor)
Enter fullscreen mode Exit fullscreen mode

Output:3
6
9
12
15

count=1
floor=3
while(count<6):
    floor=floor+3
    count=count+1
    print('Floor:',floor )
Enter fullscreen mode Exit fullscreen mode

Output:Floor: 6
Floor: 9
Floor: 12
Floor: 15
Floor: 18

5) A rocket launches with a countdown using only even numbers from 10. Display the countdown.

count=10
while count >= 2:
    print(count)
    count = count - 2
Enter fullscreen mode Exit fullscreen mode

Output:10
8
6
4
2

for i in range(10, 0, -2):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output:10
8
6
4
2

Top comments (0)