Day Looping

Mary-Kate Olsen
Release: 2024-11-27 11:15:12
Original
655 people have browsed it

Day  Looping

pycache:

pycache is a directory created by Python to store compiled versions of your Python scripts. These files have a .pyc extension and are generated automatically when a Python script is executed.

*Programming Rules: *
1) Known to Unknown
2) Don't think about entire output
3) Think ONLY about very next step
4) Introduce variable if necessary
5) Observe program closely

Write a program to print 5 consecutive number.

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

if statements, each printing the value 1 followed by a space (end=' ') if count <= 5. After each if block, count is incremented by 1.

1 1 1 1 1
Copy after login
Copy after login

Alternate implementation:

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

If the goal is to repeat this logic in a more concise way, a loop can be used

1 1 1 1 1
Copy after login
Copy after login

Short form of multiple if is called while.

The print() function uses two optional parameters, sep and end, to control the formatting of its output.

sep(Separator):

Specifies the string inserted between multiple arguments passed to print().

print("Hello", "World", sep=", ")
Copy after login
Hello, World
Copy after login
print("Kuhan",'guru','varatha',sep='-')
Copy after login
Kuhan-guru-varatha
Copy after login

end(End Character):
Specifies the string appended at the end of the output

print("Hello", end="!")
print("World")

Copy after login
Hello!World
Copy after login

Combining sep and end:

print("Python", "is", "fun", sep="-", end="!")
Copy after login
Python-is-fun!
Copy after login

Types of argument:

Positional argument:

Arguments passed in the exact order as specified in the function definition.

def add(no1,no2):
    print(no1+no2)
add(10,20)
Copy after login

This calls the function add, passing 10 as no1 and 20 as no2.

30
Copy after login

Variable-Length Arguments:

Allow passing a variable number of arguments.

How to get only date from datetime module python:

from datetime import date
current_date=(date.today())

print("Current Date:",current_date)
Copy after login
Current Date: 2024-11-25
Copy after login

The above is the detailed content of Day Looping. 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