Posts

Showing posts from September, 2020

If __name__==__main__ usage & necessity

def printary (string): return f"Ye string harry ko de de thakur { string } " def add (num1, num2): return num1 + num2 + 5 print ( "aand the name is" , __name__) if __name__ == '__main__' : print (printary( "Harry1" )) o = add( 4 , 6 ) print (o) # For logic statements and the syntax is if __name__ == "__main__" : #Logic Statement

How Import Works In Python?

# import flask as fl # print(fl.__version__) import sys print (sys.path) Code file1 as described in the video from sklearn.ensemble import RandomForestClassifier print(RandomForestClassifier()) import file2 print(file2.a) file2.printjoke("This is me")   Code file2 as described in the video   a =7 def printjoke(str):     print(f"this function is a joke {str}")     

Enumerate Function

  l1= { "Bhendi" , "Laddu" , "Manchurian" } for index , item in enumerate (l1): if index% 2 == 0 : print ( f"Edith Please Buy { item } " )

Virtual Environment & Requirements.txt

 To open Powershell in the folder: Click Shift + Right CLick and then select Open Powershell here. To make a virtual environment : pip install virtualenv Then enter name: virtualenv virtualenv_name To Activate:  .\virtualenv_name\Scripts\activate To Deactivate: (virtualenv_name) deactivate To get requirements.txt: pip freeze  >  requirement.txt This will make a notepad file. To Install a specific version: pip install package_name  = =  version If you want to install the whole requirements.txt file: pip install -r .\requirements.txt

Time Module In Python

  import time intial= time.time() # print(intial) k= 0 while k< 45 : print ( "This Is Aryan" ) k=k+ 1 print ( "While Loop Took This Much Time:" , time.time()- intial, "Seconds" ) intial2= time.time() for i in range ( 45 ): print ( "This Is Aryan" ) print ( "For Loop Took This Much Time:" , time.time()- intial2, "Seconds" ) # Syntax for time sleep is : # time.sleep(5) #Here value in bracket is the seconds, this is used to make user wait #Syntax For Local Time Is: localtime=time.asctime(time.localtime(time.time())) print (localtime)

*args and **kwargs In Python

  # def function_name_print(a, b, c, d, e): # print(a, b, c, d, e) def funargs (normal, *argsrohan, **kwargsbala): print (normal) for item in argsrohan: print (item) print ( " \n Now I would Like to introduce some of our heroes" ) for key, value in kwargsbala.items(): print ( f" { key } is a { value } " ) # function_name_print("Harry", "Rohan", "Skillf", "Hammad", "Shivam") har = [ "Harry" , "Rohan" , "Skillf" , "Hammad" , "Shivam" , "The programmer" ] normal = "I am a normal Argument and the students are:" kw = { "Rohan" : "Monitor" , "Harry" : "Fitness Instructor" , "The Programmer" : "Coordinator" , "Shivam" : "Cook" } funargs(normal, *har, **kw)

F-String

#F-String is used to fast our work and add values and all to a string   me= "Aryan" a1= 3 a= f"This Is me { me } { a1 } " print (a)

Using Python External & Built In Modules

  # Using Python External & Built In Modules import random random_number = random.randint( 0 , 2 ) # print(random_number) rand = random.random() * 100 # print(rand) list = [ "Star Plus" , "DD1" , "Aaj Tak" , "CodeWithHarry" ] choice = random.choice(list) print (choice)

Recursions in Python

Factorial Numbers: # Factorial Numbers are n* (n-1) like: #4!= 4*3*2*1 #!1 = 1 #0! = 1   def factorial (n): if n== 0 or n== 1 : return 1 return n * factorial(n- 1 ) print ( "Enter The Number" ) fac = factorial( int ( input ())) print (fac)

Recursions: Recursive Vs Iterative Approach

  # n! = n * n-1 * n-2 * n-3.......1 # n! = n * (n-1)! def factorial_iterative (n): """ :param n: Integer :return: n * n-1 * n-2 * n-3.......1 """ fac = 1 for i in range (n): fac = fac * (i + 1 ) return fac def factorial_recursive (n): """ :param n: Integer :return: n * n-1 * n-2 * n-3.......1 """ if n == 1 : return 1 else : return n * factorial_recursive(n - 1 ) # 5 * factorial_recursive(4) # 5 * 4 * factorial_recursive(3) # 5 * 4 * 3 * factorial_recursive(2) # 5 * 4 * 3 * 2 * factorial_recursive(1) # 5 * 4 * 3 * 2 * 1 = 120 # 0 1 1 2 3 5 8 13 def fibonacci (n): if n == 1 : return 0 elif n == 2 : return 1 else : return fibonacci(n - 1 ) + fibonacci(n - 2 ) number = int ( input ( "Enter then number" )) # print("Factorial Using Iterative Method", factorial_iterat...

Scope, Global Variables and Global Keyword

  # Scope, Global Variables and Global Keyword l= 10 # Global: Used by everyone, Sarkar Ka Money.(Anyone can use) def function1 (n): global l l = 5 #Local: Onyl the function can use it, Personal Money. m = 8 #Local l= l+ 45 print (l, m) print (n, "I have Printed" ) function1( "This Is Me" ) # Global is read only variable. We can't Add,multiply, or do anything else. # Global Keyword is the permission/order to change the Global variable

Exercise 5 Health Management System

# Health Management System # 3 clients - Harry, Rohan and Hammad # Total 6 files # write a function that when executed takes as input client name # One more function to retrieve exercise or food for any client # Code: import datetime def gettime (): return datetime.datetime.now() def take (k): if k== 1 : c= int ( input ( "Dear Client \n " "Pls Enter 1 for Excersise and 2 for Food \n " )) if (c== 1 ): value= input ( "Type here \n " ) with open ( "Harry Exercises.txt" , "a" ) as op: op.write( str ([ str (gettime())])+ ": " +value+ " \n " ) print ( "Successfully written" ) elif (c== 2 ): value = input ( "type here \n " ) with open ( "Harry Food.txt" , "a" ) as op: op.write( str ([ str (gettime())]) + ": " + value + " \n ...

Seek(), tell() & More On Python Files

 #30 # f= open("aryan.txt") # print(f.tell()) # print(f.readline()) # print(f.tell()) # print(f.readline()) # print(f.tell()) # print(f.readline()) # f.close() #Seek is used to determine from where to read for ex if we type 0 so it will read from 0 character and so on. f= open ( "aryan.txt" ) print (f.readline()) f.seek( 11 ) print (f.readline()) #31 Using With Block with open ( "aryan.txt" ) as f: a= f.readlines() print (a)

Python File IO Basics

  Python File IO Basics  #25 Non volitile: which does not gets vanish when laptop is turned off . volitile:  which does gets vanish when laptop is turned off . r : r mode opens a file for read.-default w : w mode opens a file for read. x : x creates a new file. It does not work for an already existing file, as in such cases the operation fails. a : a for append, which means to add something to the end of the file. It just adds the data we like in write(w) mode but instead of overwriting it just adds it to the end of the file t : used to open our file in text mode. It deals with the file data as a string.-default b : b for binary and this mode can only open the binary files, that are read in bytes. The binary files include images, documents, or all other files that require specific software to be read. + :we can read and write a file simultaneously. The mode is mostly used in cases where we want to update our file.  #26  Open(), Read(), Readline() #To read: ...
  #24 Try Except Exception Handling In Python num1= input ( "Enter num 1 \n " ) num2= input ( "Enter num 2 \n " ) try : print ( "the two number sum is" , int (num1)+ int (num2)) except Exception as e: print (e) print ( "This line is very important" ) #23 Functions And Docstrings # For using docstring we have to use three times ". Like: def function1 (a,b) : """"This Function Is Used To Calculate Average""" average = (a+b)/ 2 return average # The following2 lines will print average # v= function1(5,7) # print(v) print (function1. __doc__ )
Operators Types : Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Identity Operators Membership Operators Bitwise Operators Assignment Operators:             The assignment operator is used to assign values to a variable. In some cases, we have to assign a variable’s value to another variable, in such cases the value of the right operand is assigned to the left operand. One of the basic signs from which we can recognize an assignment operator is that it must have an equal-to(=) sign. Some commonly used assignment operators include +=, -=, /=, etc.   Comparison Operators:             They are also known as relational operators. They compare the values on either side of the operator and decide the relation among them. Commonly used comparison operators include ==, >, < , >=,etc.   Logical Operators:      ...
  Python Exercise 1 - Apni Dictionary For example: The user inputs the word : “programming” The output will be:  "the process of writing computer programs" Your main focus should be towards writing a neat and efficient code, using only the knowledge from our previously done tutorials.
Image
 Calculator print ( "Enter First Number" ) var1= int ( input ()) print ( "Enter Second Number" ) var2= int ( input ()) var3= str ( "YYY" ) var4= str ( "Ok You Are Correct" ) print ( "Sum of the values is :" ) print (var1+var2) Output Enter First Number: Both Numbers Entered: Result:
 Making A Guessing Game n= 18 number_of_guesses= 1 print ( "Number of guesses is limited to only 9 times: " ) while (number_of_guesses<= 9 ): guess_number = int ( input ( "Guess the number : \n " )) if guess_number< 18 : print ( "you enter less number please input greater number. \n " ) elif guess_number> 18 : print ( "you enter greater number please input smaller number. \n " ) else : print ( "you won \n " ) print (number_of_guesses, "no.of guesses you took to finish." ) break print ( 9 -number_of_guesses, "no. of guesses left" ) number_of_guesses = number_of_guesses + 1 if (number_of_guesses> 9 ): print ( "Game Over" )