Posts

Showing posts from June, 2024

Math functions in ‘math’ module -II

Image
               Basic mathematical functions in math module are explained in part1 post. https://www.blogger.com/blog/post/edit/5251857942821229/5740320177801086000 . Remaining mathematical functions are explained in this blog post. Trigonometry functions in ‘math’ module:             Trigonometry is a field of mathematics which deals with angles. To measure angles, radian and degree are used. In ‘math’ module, there are two functions to convert radian to degree and degree to radian. #import the math module import math #get the input of degree value and store as integer in ‘de’ de = int(input("Enter the degree value")) #print the converted radian value print(math.radians(de)) #get the input value as float and store it as float in ‘ra’ ra = float(input("Enter the radian value")) #print the degree value print(math.degrees(ra)) Output: Some of the other functions ar...

Math function in python

Image
     Python has variety of built in modules. One of the commonly used module is “math”. It has many built in functions and constants. To use this module, use the keyword “import” import math It has some constant values that are listed below. #This program uses the math constants. import math #math.pi gives the pi value (3.14…). print(math.pi) #math.e denotes euler’s number. print(math.e) #math.tau represents tau. print(math.tau) #math.inf gives floating point infinity value. print(math.inf) #math.nan provides floating point not a number value. print(math.nan) When executing this program, the user gets the below output. output: 3.141592653589793 2.718281828459045 6.283185307179586 inf nan   math module’s functions:             math module has variety of built in functions for performing basic mathematical calculations to advanced calculations. Here, some of the b...

Python scope

Image
   Scope of a variable depends on the place, where it is defined and where it is used. Based on this, variables have categorized into following types. ·        Local ·        Non local ·        Global 1. Local variables:               These are the normal variables that any user use. Let, a variable t is defined inside a function. Its scope is applicable within the function or file. Eg: a = 56 print(a) Here, a is the local variable. its scope is within this program. Next, local variable   is defined inside a function is given below. def   its_fn():      b = 99      print(b) #function call is given below. its_fn() while executing this program, you get the below output.   2.Non local variables.       ...

How to do Exception handling in python??

Image
               A good developer knows about the structure of proper coding. It’s always good to practice exception handling mechanisms. In python, we have the following mechanisms for exception handling. try except try, except, else Using finally 1.try except exception handling:               This is the common method used in many languages. Try is the beginning keyword. It follows a coding block and end by a keyword “except” follows a statement. Syntax: try:   coding block except:   a line of statement. For example, #Get a float number a = float(input("Enter the number")) #Create a try except block using the print statement. try :      print(a/0) except:      print("Exception occurred") The outcome of this program is Enter the number 45.7 Exception occurred 2.try,except,else:  ...

Common errors in python

Image
               These are special conditions which may occur due to some situations. Some of the commonly known errors are listed below. 1.        Assertion error 2.        Attribute error 3.        Value error 4.        Tab error 5.        Import error 6.        Index error 1.Assertion error:               When an assertion statement fails, this error occurs. Eg:   a = 5   b =   10   assert b < a , “b is less than a” when executing this, you get the assertion error. 2. Attribute Error:               It occurs while assigning values to attributes or related operations. Eg:  Here, app...