Posts

Showing posts from June, 2022

My very basic calculator

It can perform arithmetic operations on two numbers at a time... x = int ( input ( "enter 1st number: " )) y = input ( "What you want to do? " ) z = int ( input ( "enter 2nd number: " )) if y == "+" :     print ( x + z ) elif y == "-" :     print ( x - z ) elif y == "*" :     print ( x * z ) elif y == "/" :     print ( x / z )

Lambda function or Anonymous function

a , b = 3 , 4 d = lambda a , b : a + b c = d ( a , b ) print ( c ) Result = 7

Creating Fibonacci Sequence in python

In mathematics, the Fibonacci numbers form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1, although some authors omit the initial terms and start the sequence from 1 and 1 or from 1 and 2.  Starting from 0 and 1, the next few values in the sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... n = int ( input ( "Number(N) of elements in fiboSeries (N>=2): \n " )) fiboSeries = [ 0 , 1 ] if n > 2 :     for i in range ( 2 , n ):         nextElement = fiboSeries [ i - 1 ] + fiboSeries [ i - 2 ]         fiboSeries . append ( nextElement ) print ( fiboSeries )      

Generator Function

 The generator function uses the 'yield' keyword. mylist = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] def eo ():     for item in mylist :         if item % 2 == 0 :             yield f " { item } is even"         else :             yield f " { item } is odd"             for i in eo ():     print ( i )  

Countdown function

  def countdown ( n ):     while n > 0 :         print ( n )         n -= 1 countdown ( 6 )

Creating Pandas DataFrames

 Creating a DataFrame by using a dictionary: familyDict = {     "Names" : [ "Rehman" , "Wadud" , "Wahab" , "Aasia" , "Memoona" ],     "Age" : [ 35 , 26 , 30 , 24 , 39 ],     "Qualification" : [ "Pharm-D" , "B-Tech" , "Civil" , "BA" , "Islamyat" ],     "Relation" : [ "Son" , "Son" , "Son" , "Daughter" , "Daughter" ],     "Status" : [ "Married" , "Married" , "Married" , "Married" , "Married" ]     } import pandas as pd family_table = pd . DataFrame ( familyDict ) family_table . index = [ 1 , 2 , 3 , 4 , 5 ] # changes the index pattern of the table print ( family_table ) Creating a DataFrame by importing a CSV file using Pandas import pandas as pd notes = pd . read_csv ( "CSV-notes.csv" ) print ( notes )

Numpy arrays

  height = [ 1.87 ,   1.87 , 1.82 , 1.91 , 1.90 , 1.97 ] weight = [ 81.65 , 97.52 , 95.25 , 92.98 , 86.18 , 82.65 ] import numpy as np np_height = np . array ( height ) np_weight = np . array ( weight ) bmi = np_weight / np_height ** 2 print(bmi) print ( bmi > 23 ) # returns boolean values print(bmi[ bmi > 23 ]) # returns bmi with values > 23

Deleting and Adding items to a dictionary

mydict = {     "table" : "wood" ,     "bangle" : "gold" ,     "window" : "glass" ,     "body" : "mud" ,     "shopper" : "plastic" ,     "protein" : "amino acids" } del mydict [ "protein" ] or mydict . pop ( " protein " ) mydict [ "feminine" ] = "love"  

Iterating over Python Dictionaries

Python dictionaries are iterables. mydict = {     "table" : "wood" ,     "bangle" : "gold" ,     "window" : "glass" ,     "body" : "mud" ,     "shopper" : "plastic" ,     "protein" : "amino acids" } for item , material in mydict . items ():     print ( f "The { item } is made out of { material } ." ) Result: The table is made out of wood. The bangle is made out of gold. The window is made out of glass. The body is made out of mud. The shopper is made out of plastic. The protein is made out of amino acids.  

Splitting a string into list items

The split method splits the string into a bunch of strings grouped together in a list . name = "abdur rehman" name = name . split ( " " ) print ( name ) Result: ['abdur', 'rehman']

Reversing a string in Python

  name = "abdur" print ( name [::- 1 ]) Result: rudba

Formatting list into a string in Python

  tup = ( "Abdur" , 35 , "coder" , 4.5 ) scr = "Hi this is %s aged %s . I am %s by profession and i have %s years of work experience." print ( scr % tup )

Various methods of String Formatting in Python

name = "Abdur" age = 35 print ( "Hello %s !" % name ) #  %s used for strings print ( "Age is %d " % age )   #  %d used for numbers print ( " %s is %d years old." %( name , age )) print ( "Hello {} !" . format ( name )) print ( f "Hello { name } !" )  

Applying 'Reduce' to a list

The following code will sum all the numbers in the list called 'a'.  sum = lambda a , b : a + b a = [ 1 , 2 , 3 , 4 , 15 ] from functools import reduce b = reduce ( sum , a ) print ( b ) The following code will return the maximum number from a list of numbers; mylst = [ 1 , 2 , 3 , 4 , 5 ,6 6 , 7 , 8 , 9 ]     from functools import reduce b = reduce ( max , mylst ) print ( b ) Result = 66

Applying a function to a list by MAP method

 Syntax: map(function, list) def cube ( num ):     return num * num * num # method 01 l1 = [ 2 , 4 , 6 ] l2 = [] for item in l1 :     l2 . append ( cube ( item ))     print ( l2 ) # method 02 print ( list ( map ( cube , l1 )))

Applying 'filter' to a list

  def name ( item ):     if item == "abdur" :         return True     else :         return False     list1 = [ "abdur" , "michael" , "vikky" ]   b = list ( filter ( name , list1 )) print ( b )

FORMAT method in Python

 This is the older method to write f "string" num = 44 name = "Ronaldo" career = "10 years" print ( "The player {} whose assigned number is {} is playing football since {} " . format ( name , num , career ))

Join method in Python

 Join method in Python applied on a list of names: mylist = [ "abdur" , "rehman" , "qadeer" , "cats" , "dogs" ] oneLiner = " & " . join ( mylist ) print ( oneLiner ) The above code returns the following; abdur & rehman & qadeer & cats & dogs  Join method in Python applied on a list of numbers: mylist = [ str ( i * 7 ) for i in range ( 1 , 11 )] mylist = [7, 14, 21, 28, 35, 42, 49, 56, 63, 70] newlist = " \n " . join ( mylist ) print ( newlist ) The above code returns the following; 7 14 21 28 35 42 49 56 63 70

Lambda function in Python

 Defining a function in a single line in Python multiply = lambda a , b : a * b sum = lambda a , b : a + b div = lambda a , b : a / b subt = lambda a , b : a - b square = lambda a : a ** 2 cube = lambda a : a ** 3   sqrt = lambda a : a ** 0.5 print ( multiply ( 2 , 3 )) print ( sum ( 2 , 3 )) print ( div ( 2 , 3 )) print ( subt ( 2 , 3 )) print ( square ( 2 )) print ( cube ( 2 )) print ( sqrt ( 9 ))

Creating Multiplication Table through List Comprehension in Python

  tableOf = int ( input ( "Which table? " )) list = [ tableOf * i for i in range ( 1 , 11 )] print ( list )

Printing specific elements from a list

The following program will print the 1st, 3rd and 5th elements of the list only. list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] for index , item in enumerate ( list ):     if index == 0 or index == 2 or index == 4 :         print ( index , item )