Posts

Showing posts from September, 2022

How to make factorial of a number in python? Learning Python - 6th Night - 2

 Use this function to get factorial of a number x def fact (x): for i in range ( 1 ,x): x *= i return x print (fact( 6 ))

Fibonacci series in Python - Learning Python (6th Night)

 Just run this code in any IDE, and see the output: def fib (n): a = 0 b = 1 print (a) print (b) for i in range ( 1 ,n): c=a+b print (a+b) a = b b = c fib( 10 )

Learning Python - 6th day (Noon)

 Watched this video: Click here I have learnt about global and local variables in this tutorial. I know that I have functions in Python. And I also know what they can do.  But I have a question, can I create new variables in a function? Yes I can! Example: | def myFunction(): |      a = 10 |    print(a)    Output: 10 Okay, it works then! But the variable 'a' is created within the function myFunction() , right? What if the variable 'a' is already defined outside the function myFunction() ? Will it change the value of previous variable? Answer is No! Any new variable that is created within the function, or any variable which is used inside the function is the local variable of that function. No other code outside that function can use them.  For example: 1|  def myFunction(): 2|     a = 10 3|     print(a)    4| 5|  print(a)    Output: #Error: 'a' is not defined in line 5 Here we can call 'a' is a local variable for myFunction() What if we define a outside

Learning Python - 6th day (Morning)

 I watched this awesome tutorial: Click here Today I have learnt about new things. Yes completely new Python codes which are not even available in Java or C++. After this I love Python more than before. I have learnt 3 things:     1. Position :             Yesterday I studied about functions more and more and I have got to know about an issue.             Let's imagine, I have function called printNames(name1,name2)             Let's say name 1 should be Harry             And name 2 should be Steve             So, the function and python code will be,                          def printNames(name1,name2):                     print('Name 1 is: ', name1)                     print('Name 2 is: ', name2)             printNames('Harry','Steve')                            Output: Name 1 is: Harry                             Name 2 is: Steve             Here, if I suddenly put printNames('Steve', 'Harry') the output will be reversed as wel

Learning Python - 5th Day (Part 1)

 Watched this tutorial: Click here I learned about functions() in python. Just like java and c++ Python also has functions.  Let me demonstrate what a function is, Imagine you have 5 books. You want to know the name of all books with just one line of code. So you might write the code like: if I give a command:       Tell me the name of first book       Tell me the name of second book       Tell me the name of third book       Tell me the name of forth book       Tell me the name of fifth book This will work, but you can see the code looks larger. It would be really helpful if I just write (tell_all_book_names) and all books name were given. Well python does not have that function built in for you. But you can make your own function. Example:  tell_all_book_names:     Tell me the name of first book     Tell me the name of second book     Tell me the name of third book     Tell me the name of forth book     Tell me the name of fifth book if I give a command:     tell_all_book_names Does