Function Use: To print all the directories in a folder/sub-folder or directory import os def get_list_or_dir(local_dir): for dir_or_file in os.listdir(local_dir): if os.path.isdir(dir_or_file): path = os.path.join(local_dir, dir_or_file) print(dir_or_file, 'This is a directory') get_list_or_dir(path) else: print(dir_or_file) Path = os.getcwd() or define the path from where you need a file Function Call: get_list_or_dir(path)
How to print the below pattern in Python:
* * * *
* * * *
* * * *
* * * *
I will be showing 2 ways to print the above pattern, but there are more ways; you can explore it by practising yourself.
Before starting this i want to show you some of the trick that you should know. If you want to print same number, word or anything number of times, you can the use the below code.
>>> print(4* "Avenger ")
Avenger Avenger Avenger Avenger
Here i wanted to print Avenger 4 times than i just wrote 4*"Avenger " in the print statement and i got the desired output.
So, we are good to begin printing the above pattern in python.
Method 1:
>>> n = 4
>>> for i in range(0, n):
print(n*"* ")
* * * *
* * * *
* * * *
* * * *
I guess i don't need to explain it further, I wanted to print star in 4 times so i just wrote print(n* "* ") to print the star 4 times in a row and i ran the loop 4 times by using "for" loop.
Method 2:
>>> n = 4
>>> for i in range(0,n):
j = 0
while j < n:
print("* ", end = "")
j += 1
print()
* * * *
* * * *
* * * *
* * * *
Here we have used end="' ". You can follow this blogpost to learn about end parameter.
Keep reading Python an hour a day to learn simple Python tricks, tips and programme.
* * * *
* * * *
* * * *
* * * *
I will be showing 2 ways to print the above pattern, but there are more ways; you can explore it by practising yourself.
Before starting this i want to show you some of the trick that you should know. If you want to print same number, word or anything number of times, you can the use the below code.
>>> print(4* "Avenger ")
Avenger Avenger Avenger Avenger
Here i wanted to print Avenger 4 times than i just wrote 4*"Avenger " in the print statement and i got the desired output.
So, we are good to begin printing the above pattern in python.
Method 1:
>>> n = 4
>>> for i in range(0, n):
print(n*"* ")
* * * *
* * * *
* * * *
* * * *
I guess i don't need to explain it further, I wanted to print star in 4 times so i just wrote print(n* "* ") to print the star 4 times in a row and i ran the loop 4 times by using "for" loop.
Method 2:
>>> n = 4
>>> for i in range(0,n):
j = 0
while j < n:
print("* ", end = "")
j += 1
print()
* * * *
* * * *
* * * *
* * * *
Here we have used end="' ". You can follow this blogpost to learn about end parameter.
Keep reading Python an hour a day to learn simple Python tricks, tips and programme.
Comments
Post a Comment