Latest Web Stories

1

Exploring Smartphone Influence on the Brain: Neurological Perspectives

2

Angular vs. React: Which One Should You Choose?

3

Discover Serenity: Your Guide to Mindfulness Apps

4

Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps

5

Discover How Smartwatches Measure Blood Pressure: Explained Simply

6

13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!

7

Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo

8

Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!

9

Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals

10

Affordable VR Adventures: The Best Budget VR Headsets

11

Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying

12

Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More

13

Discover Exciting New Games on NVIDIA GeForce NOW!

14

Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games

15

The Future of iPhones: Apple's Bold Step into AI with DarwinAI

16

Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!

17

Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!

18

Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches

19

Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024

20

Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!

21

Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent

22

Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer

23

Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!

24

JavaScript Essentials: Your Quick Start to Web Coding

25

Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!

26

Gaming Joy Awaits: Explore the Best PS5 Games of 2024

27

Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!

28

Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit

29

Feel the Beat: Exploring Top Over-Ear Headphones

30

Explore the Web Development Strategies in 2024: A Developers Handbook

31

Explore Must-Play Nintendo Switch Games in 2024!

32

Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle

33

Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World

34

Discover Waze's cool new features for safer and happier drives!

35

Discover the Top Picks: Best Smartwatches for Your Lifestyle

36

Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!

37

Sound Revolution: Discover the Best Bluetooth Speakers of 2024!

38

Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks

39

Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024

40

Crack the Code: Your Guide to Computer Programming Magic in 2024

41

Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity

42

10 Best Tech Companies in Silicon Valley

43

Top 10 Web Development Interview Questions you can...

44

Learn how to Answer Tell me about Yourself

45

5 Books You Need to Read Right Now

46

25 Practical Ways to Earn Money Online

Translate this page in your preferred language:


Python Numbers: A Comprehensive Guide with Examples

Article by: Manish Methani

Last Updated: October 7, 2021 at 8:04am IST
5 min 9 sec read

  1. Numeric Data Types in Python Python has three main numeric data types:
  • Integers (int): Whole numbers, positive or negative, without a decimal point.
  • Floating-point numbers (float): Numbers with a decimal point or an exponent notation.
  • Complex numbers (complex): Numbers with a real and imaginary part.

Here are some examples:

# Integers
x = 5
y = -10
z = 0

# Floating-point numbers
a = 3.14
b = 1.23e-5

# Complex numbers
c = 3 + 4j
d = complex(1, -2)

      2. Basic Arithmetic Operations

Python supports all basic arithmetic operations for numerical data types. Here are some examples:

x = 5
y = 3

# Addition
z = x + y  # Output: 8

# Subtraction
z = x - y  # Output: 2

# Multiplication
z = x * y  # Output: 15

# Division
z = x / y  # Output: 1.6666666666666667

# Floor Division
z = x // y  # Output: 1

# Modulo
z = x % y  # Output: 2

# Exponentiation
z = x ** y  # Output: 125

       3. Type Conversion

Python allows you to convert numerical data types from one to another. Here's an example:

x = 5
y = 3.14

# Convert x to a floating-point number and add it to y
z = float(x) + y

print(z)  # Output: 8.14

In this example, we convert the integer "x" to a floating-point number using the float() function and add it to the floating-point number "y".

       4. Built-in Functions Python has several built-in functions for working with numbers. Here are some examples:

# Absolute value
x = abs(-10)  # Output: 10

# Round to a certain number of decimal places
y = round(3.14159, 2)  # Output: 3.14

# Maximum and minimum values
z = max(1, 2, 3)  # Output: 3
a = min(4, 5, 6)  # Output: 4

      5. Math Module

Python also has a math module that provides additional mathematical functions. Here's an example:

import math

# Square root
x = math.sqrt(25)  # Output: 5.0

# Trigonometric functions
y = math.sin(0)  # Output: 0.0
z = math.cos(math.pi)  # Output: -1.0

      6. Floats

Floats are used to represent real numbers with decimal points. They are also known as floating-point numbers. In Python, float is represented by float class. We can create a float variable by assigning a value with a decimal point to it.

Here's an example:

# Example of float
x = 3.14
y = 2.5
print(x)
print(y)

Output:

3.14
2.5

We can also use mathematical operations with floats:

# Example of float with mathematical operations
x = 3.14
y = 2.5
z = x + y
print(z)

Output:

5.64

Complex Numbers

Complex numbers are used to represent numbers with a real and imaginary part. In Python, complex numbers are represented by complex class. We can create a complex number by specifying the real and imaginary parts separated by a + or - sign.

Here's an example:

# Example of complex number
x = 2 + 3j
y = 4 - 2j
print(x)
print(y)

Output:

(2+3j)
(4-2j)

We can also use mathematical operations with complex numbers:

# Example of complex number with mathematical operations
x = 2 + 3j
y = 4 - 2j
z = x + y
print(z)

Output:

(6+1j)

Type Conversion

We can convert one type of number into another using type conversion functions. Here are some type conversion functions in Python:

  • int(x) converts x to an integer.
  • float(x) converts x to a floating-point number.
  • complex(x) converts x to a complex number with a zero imaginary part.
  • complex(x, y) converts x and y to a complex number with real part x and imaginary part y.

Here are some examples:

# Example of type conversion
x = 10
y = 3.14
z = 2 + 3j

# convert x to float
a = float(x)

# convert y to int
b = int(y)

# convert z to float
c = float(z)

print(a)
print(b)
print(c)

Output:

10.0
3
(2+3j)

We have covered the basics of Python Numbers in this tutorial.

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App