Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Numbers in Python

Numbers in Python

Numbers in Python

In Python, numbers can be classified into three main types:

  1. Integers (int): Whole numbers, both positive and negative, without any decimal points.

  2. Floating Point Numbers (float): Numbers that have decimal points.

  3. Complex Numbers (complex): Numbers that have a real and imaginary part.

Let’s go over each type and how they work in Python.


1. Integers (int)

Integers are whole numbers without decimal points. They can be both positive and negative.

Example:

x = 10  # An integery = -5  # A negative integerprint(type(x))  # <class 'int'>

Arithmetic Operations with Integers:

a = 15b = 4# Additionprint(a + b)  # Output: 19# Subtractionprint(a - b)  # Output: 11# Multiplicationprint(a * b)  # Output: 60# Divisionprint(a / b)  # Output: 3.75 (float result)# Integer Division (Floor Division)print(a // b)  # Output: 3 (quotient)# Modulo (Remainder)print(a % b)  # Output: 3 (remainder)

2. Floating Point Numbers (float)

Floating-point numbers are numbers with decimal points.

Example:

x = 3.14  # A floating-point numbery = -2.5  # A negative floating-point numberprint(type(x))  # <class 'float'>

Arithmetic Operations with Floats:

a = 5.5b = 2.2# Additionprint(a + b)  # Output: 7.7# Subtractionprint(a - b)  # Output: 3.3# Multiplicationprint(a * b)  # Output: 12.1# Divisionprint(a / b)  # Output: 2.5

Note:

Floating point numbers can have precision issues, especially for very small or large numbers due to how they are represented in memory.


3. Complex Numbers (complex)

Complex numbers consist of two parts: the real part and the imaginary part. In Python, complex numbers are written as a + bj, where a is the real part and b is the imaginary part.

Example:

z = 3 + 4j  # A complex numberprint(type(z))  # <class 'complex'>

Complex Number Operations:

z1 = 3 + 4jz2 = 1 + 2j# Additionprint(z1 + z2)  # Output: (4+6j)# Subtractionprint(z1 - z2)  # Output: (2+2j)# Multiplicationprint(z1 * z2)  # Output: (-5+10j)# Divisionprint(z1 / z2)  # Output: (2.2-0.4j)# Real and Imaginary Partsprint(z1.real)  # Output: 3.0print(z1.imag)  # Output: 4.0

4. Type Conversion (Casting)

You can convert between different number types in Python using type casting.

  • int(): Converts a number or a string to an integer.

  • float(): Converts a number or a string to a floating-point number.

  • complex(): Converts a number or a string to a complex number.

Examples:

# Converting float to intx = 7.6x_int = int(x)print(x_int)  # Output: 7 (decimal part is discarded)# Converting int to floaty = 5y_float = float(y)print(y_float)  # Output: 5.0# Converting string to numbers = "3.14"s_float = float(s)print(s_float)  # Output: 3.14

5. Number Functions in Python

Python provides some built-in functions for working with numbers.

  • abs(x): Returns the absolute value of x.

  • round(x, n): Returns x rounded to n decimal places.

  • pow(x, y): Returns x raised to the power of y (i.e., x^y).

  • max() and min(): Returns the maximum and minimum values from a sequence.

  • sum(): Returns the sum of elements in an iterable.

Examples:

# Absolute Valueprint(abs(-10))  # Output: 10# Round a numberprint(round(3.14159, 2))  # Output: 3.14# Powerprint(pow(2, 3))  # Output: 8 (2^3)# Maximum and Minimumprint(max(1, 2, 3, 4, 5))  # Output: 5print(min(1, 2, 3, 4, 5))  # Output: 1# Sumnumbers = [1, 2, 3, 4, 5]print(sum(numbers))  # Output: 15

6. Mathematical Operations

Python has a module called math that provides mathematical functions.

To use it, you need to import it:

import math# Square rootprint(math.sqrt(16))  # Output: 4.0# Exponential function (e^x)print(math.exp(2))  # Output: 7.3890560989306495# Factorialprint(math.factorial(5))  # Output: 120# Trigonometric functionsprint(math.sin(math.pi/2))  # Output: 1.0print(math.cos(math.pi))    # Output: -1.0

7. Random Numbers

Python's random module allows you to generate random numbers.

  • random.randint(a, b): Returns a random integer in the range [a, b].

  • random.uniform(a, b): Returns a random floating-point number in the range [a, b].

  • random.random(): Returns a random floating-point number between 0 and 1.

Examples:

import random# Random integer between 1 and 10print(random.randint(1, 10))# Random floating-point number between 1 and 10print(random.uniform(1, 10))# Random number between 0 and 1print(random.random())

Summary of Numbers in Python

TypeExampleDescription
int5, -10Integer numbers without decimal points.
float3.14, -0.5Numbers with decimal points.
complex3 + 4jNumbers with real and imaginary parts.
Functionsabs(), round()Functions for absolute value, rounding, etc.

Numbers in Python are versatile and easy to use, whether you're working with simple integers, floating-point numbers, or even complex numbers.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql