Python Day-redefined modules,While loop,Task

Mary-Kate Olsen
Release: 2024-11-28 05:07:18
Original
960 people have browsed it

Python Day-redefined 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)  
Copy after login

Output:

guru@guru-Aspire-A315-58:~/Desktop/Guru$ python3 user.py guru kuhan varatha pritha krihsnaveni
['user.py', 'guru', 'kuhan', 'varatha', 'pritha', 'krihsnaveni']
Copy after login

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")
Copy after login

Output:

Good Morning
Hello

Copy after login

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
Copy after login

Case:2

count = 1
while count<=5:
    print(1, end=' ')
    count=count+1 #count = 2
Copy after login

Output for both case:1,2

1 1 1 1 1
Copy after login

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)
Copy after login

Output:

Date:  2024-11-22

Copy after login

The above is the detailed content of Python Day-redefined modules,While loop,Task. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template