• Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials – Company wise
      • Infosys Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Dell Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • TCS Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation – E Books
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Register
    • Login
  • [email protected]
  • ..
Login / Register
Apply Now
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
0

Cart

Python Mathematical Functions

  • January 24, 2020
  • CODE OF GEEKS
  • 0


Converting a number from octal binary and hexadecimal system to decimal number system.

n1 = 0o17 # representation of octal numbers

n2 = 0b1110010 # representation of binary numbers

n3 = 0x1c2 # representation of hexadecimal number

We can do this with the help of int() method like int(n1), int(n2), int(n3).

# representation of octal numbers
n1 = 0o17
# representation of binary numbers
n2 = 0b1110010
# representation of hexadecimal number
n3 = 0x1c2
print("Octal to Decimal : ", int(n1))
print("Binary to Decimal : ", int(n2))
print("Hex to Decimal : ", int(n3))

Converting a decimal integer to octal, binary and hexadecimal system.

bin() : For decimal to binary conversion.

oct() : For decimal to octal conversion.

hex() : For decimal to hexadecimal conversion

num = 10
print("10 in octal form : ", oct(num))
print("10 in hexadecimal form : ", hex(num))
print("10 in binary form : ", bin(num))

You can remove the first two character (0o, 0x, 0b) for each form to get the formatted result. Let us modify the above code to achieve this.

Note : We just did some casting here, we’ll learn this in upcoming lessons.

num = 10
print("10 in octal form : ", str(oct(num)).replace('0o', ''))
print("10 in hexadecimal form : ", str(hex(num)).replace('0x', ''))
print("10 in binary form : ", str(bin(num)).replace('0b', ''))

Cool Mathematical methods

ceil(x) : It raises x value to the next higher integer value. For example, ceil(4.5) gives 5.

floor(x) : It decreases x value to previous integer value. For example, floor(4.5) gives 4.

degrees(x) : It converts angle value x from radians to degrees.

radians(x) : It converts x value from degree to radians.

sin(x) : It gives a sine value of x.

cos(x) : It gives a cosine value of x.

tan(x) : It gives a tan value of x.

exp(x) : It gives exponential of x.

fabs(x) : It gives absolute value of x. Like fabs(-4.53) gives 4.53.

factorial(x) : It gives the factorial of x.

fmod(x,y) : It gives remainder of division of x & y. Like, fmod(13.5,3) gives 1.5.

fsum(val) : It gives the accurate sum of floating point values.

log10(x) : It gives base-10 logarithm of x.

sqrt(x) : It gives the square-root of number x.

pow(x,y) : It raises x value to the power y.

pow(x,y,z) : It raises x value to the power y mod z.

gcd(x,y) : It is used to find the greatest common divisor of x & y.

trunc(x) : It returns real value of x is truncated to integer value. Like trunc(43.545) returns 43.

isnan(x) : It returns True if x is not a number.

eval(expression) : It returns evaluated arithmetic expression. Like, eval(3*7) gives 21.



# Illustration of mathematical methods :

# We need to import python in-built module 'math' to access mathematical methods

import math as m # creating 'm' as alias(nickname) for 'math'

print("ceil(4.5) : ", m.ceil(4.5))
print("floor(4.5) : ", m.floor(4.5))
print("degrees(180/3.14) : ", m.degrees(180/3.14))
print("radians(90) : ", m.radians(90))
print("sin(30) : ", m.sin(30))
print("cos(45) : ", m.cos(45))
print("tan(60) : ", m.tan(60))
print("exp(2) : ", m.exp(2))
print("fabs(-2.86) : ", m.fabs(-2.86))
print("factorial(6) : ", m.factorial(6))
print("fmod(17,2) : ", m.fmod(17,2))
print("log10(2) : ", m.log10(2))
print("sqrt(20) : ", m.sqrt(20))
print("pow(2,5) : ", m.pow(2,5))
print("gcd(50,100) : ", m.gcd(50,100))
print("trunc(7.677) : ", m.trunc(7.677))
print("isnan(2) : ", m.isnan(2))
print("eval('(2*3)+(9/3)') : ", eval("(2*3)+(9/3)"))

That’s all, see you in the next lecture.



Tags: binary and hexadecimal system.binary to decimal pythonceil() in pythonConverting a decimal integer to octalfloor() pythonoct to int pythonpython math functions
  • Previous PYTHON CODEBOOK : E-BOOK
  • Next Operators in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Table of Content – Python

  1. Introduction to Python Programming
    • Python and its features
    • Key Differences : C vs Python
    • Key Differences : Java vs Python
    • Python Flavors
    • PVM and Memory Management in Python

  2. Python Programming Fundamentals
    • 'Hello World' using Python
    • Comments in Python
    • Variables and Garbage Collection in Python
    • Datatypes in Python
    • Determining the Datatype of Literal | type() method
    • Tokens in Python
    • Keywords in Python
    • Identifiers and Naming Convention in Python
    • Literals in Python

  3. Python Operators
    • Operators in Python
    • Python Mathematical Functions

  4. Python User Inputs
    • Taking Inputs | input() function in Python

  5. Python Control Statements
    • Control Statements in Python
    • If-elif-else Statements in Python
    • Looping in Python
    • while loop in Python
    • for loop in Python
    • Infinite loops in Python
    • Nested loops in Python
    • Break Statement in Python
    • Continue Statement in Python
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

What is Cloud Computing | Cloud Computing Applications | Cloud Development Models| SaaS vs PaaS vs IaaS
  • October 23, 2021
What is Amazon Web Services | AWS Regions & AZs | Different AWS Services
  • October 19, 2021

Copyright 2021 CODE OF GEEKS. All Rights Reserved.

  • →