Python Day-9 Predefined modules,While loop,Task

guru_prasanna_01

Guru prasanna

Posted on November 24, 2024

Python Day-9 Predefined modules,While loop,Task

Predefined modules

sys module:(sys.argv)

In this module sys.argv is used to show output as lists.
For example:
Input:

import sys
print(sys.argv)  
Enter fullscreen mode Exit fullscreen mode

Output:

guru@guru-Aspire-A315-58:~/Desktop/Guru$ python3 user.py guru kuhan varatha pritha krihsnaveni
['user.py', 'guru', 'kuhan', 'varatha', 'pritha', 'krihsnaveni']
Enter fullscreen mode Exit fullscreen mode

So this argv function is used to show outputs as a list by entering data after python3 with space.

Time module: (time.sleep)
This sleep is used for time delay which means pauses in execution.It will be mostly used in automation.
Input:

import time
print("Good Morning")
time.sleep(2)
print("Hello")
Enter fullscreen mode Exit fullscreen mode

Output:

Good Morning
Hello

Enter fullscreen mode Exit fullscreen mode

So in above output good morning will be displayed immediately,But hello will be delayed for 2 seconds then it will display.

Note:If we are importing any module we can define with any other name for that module using "as"
ex: import calculator as calc

csv-comma seperated value
json-javascript object notation

Programming basics:
1) Known to unknown
2) Don't think about entire output
3) Think only about very next step
4) Introduce variables if necessary
5) Observe program closely.

Looping:
While loop:
-->With the while loop we can execute a set of statements as long as a condition is true.
-->For writing multiple "if"conditions we can use "while"loop for repeated condition.

Ex:

To get this output 1 1 1 1 1 we have various syntax but we can use if and while now.
case:1

#using if condition

count = 1
if count<=5:
    print(1, end=' ')
    count=count+1 #count = 2
if count<=5:
    print(1, end=' ')
    count=count+1 # count = 3

if count<=5:
    print(1, end=' ')
    count=count+1 # count = 4

if count<=5:
    print(1, end=' ')
    count=count+1 # count = 5

if count<=5:
    print(1, end=' ')
    count=count+1 # count = 6
Enter fullscreen mode Exit fullscreen mode

Case:2

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

Output for both case:1,2

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

So for repeated condition we can use while loop instead of if.

Inbuilt features of print

print(*objects, sep=' ', end='\n', file=None, flush=False)

Varying arguments:(Variable length arguments)

objects:Length of arguments can vary if we define it using ().

sep=' ':In default print every output contains space if we need any other we can use (sep) and give that inside ''.

end=\n:It is used for getting output in a new line.

Task:1
To get only date from datetime module in python

from datetime import date
today=date.today()
print("Date: ",today)
Enter fullscreen mode Exit fullscreen mode

Output:

Date:  2024-11-22

Enter fullscreen mode Exit fullscreen mode

Task:2
To get age in years,months,days

from datetime import date


birth_year = 2001
birth_month = 5 
birth_day = 1 


today = date.today()
year = today.year - birth_year
month = today.month - birth_month
days = today.day - birth_day

print (f"I am {year} Years {month} Months {days} Days Old")
Enter fullscreen mode Exit fullscreen mode

Output:

I am 23 Years 6 Months 24 Days Old

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
guru_prasanna_01
Guru prasanna

Posted on November 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related