date: 26 May, 2024.
Importing from module:- To import a module(file) in our code we use: import module_name. But this may increase the size of our file. If we want only one method from the module we can use: from module import function_name. It will only import that function from the module file.
Other things I learned:
from os import system -- If we require to clear the console screen when the program is running we can import this and use: system("clear")
Docstring -- We can create documentation of our user-defined function using this. Just after the first line of function definition[def func_name():], use multiple line string("""... """") to write what function does, variables it can take, etc. Now whenever we call this function the user can see this comment on hovering over that function call.
We can use dictionary to store function names.
eg def func1:
.....
def func2:
.....
dic= { key1 : func1, #no () so it won't call the function
....
variable1=dic[key1]
this will copy the function into variable1 and we can use
variable1()Recursion - this is a process in which function keeps on calling itself and the process continues. It is just like a while loop. So we need to have a termination condition in it.
for index, item in enumerate(list):
Above is the for loop by which we can get the item and their index value in the list. Otherwise, we have to use another variable =0 and +1 in each iteration.We can't call a function before we define it.
Program I made:
Calculator app
https://replit.com/@rohitrj332024/Calculator#main.pyBlackjack -This took much of my time. But I am happy I completed it without watching the tutorial. Code may not be proper but I tried my best to use functions and comments to help myself in future. Making flowchart in advance and breaking program into smaller problems helped me a lot. I hope with practice I will improve more.
https://replit.com/@rohitrj332024/blackjack-day-7#main.py
Personal --> Coding the project took much of my time. I wanted to jump fast but I think consistency matters more. I am happy that I was able to code a game though my code may be messy or unprofessional, but coding it gave me great satisfaction. Now my brain needs some rest.
Top comments (0)