Posts

Showing posts with the label PYTHON BEGINNER'S

PYTHON BEGINNERS - 28 : MODULES

 LAST TUTORIAL OF BEGINNER'S SECTION, ABOUT MODULES: import useful_tools print ( useful_tools .roll_dice( 10 ))

PYTHON BEGINNERS - 27 : WRITING FILES

 IN THIS TUTORIAL, WE WILL LERAN TO WRTIE FILES WITH PYTHON: employee_file = open ( "employee_file.txt" , "w" ) #--> read mode,   "w" - write mode(editing)   , "a" - append mode(adding data at the end of file) , "r+" - read+write mode employee_file . write ( " \n Kelly - Customer Service" ) employee_file . close () # employee_file = open("employees1_file.txt", "w") #--> read mode,   "w" - write mode(editing)   , "a" - append mode(adding data at the end of file) , "r+" - read+write mode # employee_file.write("\nKelly - Customer Service") # employee_file.close()

PYTHON BEGINNERS - 26 : READING FILES

 HERE, YOU WILL LEARN ABOUT READING FILES THROUGH PYTHON: employee_file = open ( "employee_file.txt" , "r" ) #--> read mode, "w" - write mode(editing)   , "a" - append mode(adding data at the end of file) , "r+" - read+write mode # print(employee_file.readline()) for employee in employee_file . readlines ():     print ( employee ) employee_file . close ()

PYTHON BEGINNERS - 25 : TRY EXCEPT

 IN THIS TUTORIAL,  I LL TEACH YOU ABOUT TRY EXCEPT: # value = 10/0 --> throws an error try :     # value = 10/0 #--> invalid input     number = int ( input ( "Enter a Number" ))     print ( number ) # except ZeroDivisionError as err: #     print("Divided by Zero") except ValueError :     print ( "invalid input" )        

PYTHON BEGINNERS - 24 : COMMENTS

 HERE,WE WILL LEARN ABOUT COMMENTS: # C O M M E N T This program is C00L. print ( "Comments are fun" )

PYTHON BEGINNERS - 23 : BUILDING A TRANSLATOR

 HERE, WE WILL BUILD A TRANSLATOR: def translate ( phrase ):     translation = ""     for letter in phrase :         if letter .lower() in "aeiou" :             if letter .isupper():                 translation = translation + "G"             else :                 translation = translation + "g"         else :             translation = translation + letter     return translation print ( translate ( input ( "Enter a phrase: " )))    

PYTHON BEGINNERS - 22 : 2D LIST AND NESTED LOOPS

 HERE, WE WILL LEARN ABOUT 2D LIST AND NESTED LOOPS: number_grid = [     [ 1 , 2 , 3 ],     [ 4 , 5 , 6 ],     [ 7 , 8 , 9 ],     [ 0 ] ] # print(number_grid[1][0]) for row in number_grid :     # print(row)     for col in row :         print ( col )

PYTHON BEGINNERS - 21 : EXPONENT FUCNTION

 IN THIS TUTORIAL, WE WILL LEARN ABOUT EXPONENT FUNCTION : def raise_to_power ( base_num , pow_num ):     result = 1     for index in range ( pow_num ):         result = result * base_num     return result print ( raise_to_power ( 5 , 5 ))  

PYTHON BEGINNERS - 20 : FOR LOOP

 IN THIS TUTORIAL, YOU WILL LEARN ABOUT FOR LOOP : # for index in range(4,10): #     print(index) friends = [ "Shayan" , "Akhtar" , "Abedeen" ] for index in range ( 5 ):     if index == 0 :         print ( "first iteration" )     else :         print ( "Not first" )     # print(len(friends)) # for name in friends: #     print(name) # for index in range(len(friends)): #     print(friends[index]) # for letter in "Abedeen's Academy": #     print(letter)

PYTHON BEGINNERS - 19 : GAME

 HERE, WE WILL MAKE A SMALL  GAME: secret_word = "giraffe" guess = "" guess_count = 0 guess_limit = 3 out_of_guesses = False while guess != secret_word and not ( out_of_guesses ):     if guess_count < guess_limit :         guess = input ( "Enter guess: " )         guess_count += 1     else :         out_of_guesses = True         if out_of_guesses :     print ( "out_of_guesses, You Lose!" ) else :         print ( "You Win!" )    

PYTHON BEGINNERS - 18: WHILE LOOP

 IN THIS TUTORIAL, WE WILL LEARN ABOUT WHILE LOOP: i = 1 while i <= 10 :     print ( i )     i += 1 print ( "Done With LOOP" )    

PYTHON BEGINNERS - 17 : DICTIONARY

 IN THIS TUTORIAL, YOU WILL LEARN ABOUT DICITONARY IN PYTHON: monthConversions = {     "Jan" : "January" ,     "Feb" : "February" ,     "Mar" : "March" ,     "Apr" : "April" ,     "May" : "May" } # print(monthConversions["Dec"])   #OR --> throws an error print ( monthConversions . get ( "Dec" , "not a valid key" ))   #--> returns None

PYTHON BEGINNERS - 16 : BETTER CALCULATOR

 IN THIS TUTORIAL, WE WILL BUILT 4 FUNCTIONAL CALCULATOR: num1 = float ( input ( "Enter first number: " )) op = input ( "Enter operator: " ) num2 = float ( input ( "Enter second number: " )) if op == "+" :     print ( num1 + num2 ) elif op == "-" :     print ( num1 - num2 ) elif op == "/" :     print ( num1 / num2 ) elif op == "*" :     print ( num1 * num2 ) else :     print ( "invalid operator" )

PYTHON BEGINNERS - 15 : IF STATEMENTS COMPARISON

 IN THIS TUTORIAL, WE WILL COMPARE THE IF STATEMENTS def max_num ( num1 , num2 , num3 ):   #--> can compare all data types.     if num1 >= num2   and num1 >= num3 :         return num1     elif num2 >= num1 and num2 >= num3 :         return num2     else :         return num3 print ( max_num ( 300 , 42 , 6 ))                

PYTHON BEGINNERS - 14 : IF STATEMENT

 IN THIS TUTORIAL, YOU WILL SEE ABOUT IF STATEMENTS is_male = False is_tall = True if is_male and is_tall :   #--> true true     print ( "you are a tall male" ) elif is_male and not ( is_tall ): #--> true false     print ( "you are male but not tall" )     elif not ( is_male ) and is_tall :   #--> false true     print ( "You are not male but tall" ) else :                           #--> false false     print ( "neither a male nor tall" )    

PYTHON BEGINNERS - 13 : RETURN STATEMENT

 IN THIS TUTORIAL, WE WILL LEARN ABOUT RETURN STATEMENT def cube ( num ): #     print(num*num*num) # cube(3)                 #  OR     return num * num * num   #-->return statement     print (Code)   #--> cannot print this line becoz of return result = cube ( 4 )     print ( result )                

PYTHON BEGINNERS - 12 : FUNCTIONS

 IN THIS TUTORIAL, WE WILL LEARN ABOUT FUNCTIONS. def sayhi ( name , age ):     print ( "Hello " + name + "! You are" + age ) print ( "Top" ) sayhi ( "SHAYAN" , " 35" )   print ( "Bottom" )  

PYTHON BEGINNERS - 11 : TUPLES

 IN THIS TUTORIAL, WE WILL GO THROUGH TUPLES: coordinates = ( 7 , 2 ) #--> tuples uses this parenthesis small brackets coordinates [ 1 ] = 8 #--> tuples are immutable print ( coordinates [ 1 ])

PYTHON BEGINNERS - 10 : LISTS FUNCTIONS

 IN THIS TUTORIAL YOU WILL LEARN ABOUT LIST FUNCTIONS. NOTE: TRY ALL FUNCIONS  lucky_nums = [ 1 , 2 , 3 , 35 , 46 , 23 , 4 ] friends = [ "Kevin" , "Karen" , "Roman" , "Roman" ] # print(lucky_nums) # friends.extend(lucky_nums)  #--> adds two or more list # friends.append("SHAYAN")  #--> adds item at the last of list # friends.insert(1, "Akhtar") #--> inserts element at the given index # friends.remove("Kevin")   #--> removes the element from the list # friends.clear()   #--> removes all item from the list # friends.pop()  #--> removes last element from the list # print(friends.index("Mike")) # print(friends.count("Roman")) # friends.sort() # --> alphabetical and ascending order # print(friends) # friends.reverse() --> reverses the list # print(friends) # lucky_nums.sort() # print(lucky_nums) # lucky_nums.reverse() # print(lucky_nums) friends2 = friends . copy () #--> m...

PYTHON BEGINNERS - 09 : LIST

 IN THIS TUTORIAL, YOU WILL LEARN ABOUT LIST: friends = [ "Kevin" , "Karen" , "Roman" , 2 , 7.6 ]             # 0        1        2     3    4             # -5      -4       -3    -2   -1 friends [ 3 ] = "Reigns"             print ( friends [- 5 ]) print ( friends [ 0 ]) print ( friends [ 2 : 4 ])