DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

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

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

Alternate implementation:

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

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

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

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=", ")
Enter fullscreen mode Exit fullscreen mode
Hello, World
Enter fullscreen mode Exit fullscreen mode
print("Kuhan",'guru','varatha',sep='-')
Enter fullscreen mode Exit fullscreen mode
Kuhan-guru-varatha
Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode
Hello!World
Enter fullscreen mode Exit fullscreen mode

Combining sep and end:

print("Python", "is", "fun", sep="-", end="!")
Enter fullscreen mode Exit fullscreen mode
Python-is-fun!
Enter fullscreen mode Exit fullscreen mode

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

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

30
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
Current Date: 2024-11-25
Enter fullscreen mode Exit fullscreen mode

Top comments (0)