Python Numbers
What is Numbers in Python?
Numbers are a fundamental statistics kind used to symbolize numeric values.
There are mainly 3 sorts of numeric information kinds: integers, floats, and complicated numbers.
Integers (int):
int refers to the data type for integers. Integers are whole numbers, both positive and negative, without any decimal point. For example, 1, -5, 1000, and 0 are all integers.
Integers represent whole numbers without any decimal points.
x = 5
y = -10
Floats (float):
float refers to the data type for floating-point numbers. Floating-point numbers are numbers that contain a decimal point, such as 3.14, -0.5, 2.0, etc.
Floats are used to represent numbers with decimal points.
pi = 3.14
height = 5.9
Complex Numbers (complex):
Complex is a integrated data type to represent a complex numbers.
Complex numbers are made of a real component and an imaginary element which are each represented as floating-factor numbers.
A complex number is typically written in the form a + bj, where a is the real part, b is the imaginary part, and j represents the square root of -1 (the imaginary unit). For example, 3 + 2j, -1.5 - 4j, and 1j are all complex numbers.
Complex numbers include actual and an imaginary element. They are expressed as a bj in which a is the real part, b is the imaginary element and j is the imaginary unit.
z = 3 + 2j
Basic Numeric Operations:
You can perform basic numeric operations using arithmetic operators. These operations include addition, subtraction, multiplication, division, exponentiation, and floor division.
Here's a example overview of each:
Arithmetic Operations:
a = 10
b = 3
# Addition
addition_result = a + b # 13
# Subtraction
subtraction_result = a - b # 7
# Multiplication
multiplication_result = a * b # 30
# Division
division_result = a / b # 3.3333 (float division)
# Floor Division (returns the largest integer less than or equal to the division)
floor_division_result = a // b # 3
# Modulus (returns the remainder of the division)
remainder = a % b # 1
# Exponentiation
exponential_result = a ** b # 1000
Accessing Characters in a String:
You can access individual characters in a string using indexing. String indexing starts from 0, where the first character of the string has index 0, the second character has index 1, and so on. You can also use negative indexing, where -1 represents the last character, -2 represents the second-to-last character, and so forth.
Strings are sequences, and you can access individual characters using indexing:
word = "Python"
first_letter = word[0] # 'P'
second_letter = word[1] # 'y'
Comparison Operations:
Comparison operations in Python are used to compare two values and evaluate whether they are equal, not equal, greater than, less than, etc. These operations return a Boolean value (True or False) based on the comparison result. Python supports the following comparison operators:
x = 10
y = 5
# Equal to
is_equal = (x == y) # False
# Not equal to
not_equal = (x != y) # True
# Greater than
greater_than = (x > y) # True
# Less than
less_than = (x < y) # False
# Greater than or equal to
greater_equal = (x >= y) # True
# Less than or equal to
less_equal = (x <= y) # False
Type Conversion:
Type conversion, also known as typecasting or coercion, refers to the process of converting one data type into another in Python. Python provides built-in functions and constructors to perform type conversion between different data types.
x = 10
y = 3.14
# Convert float to int
float_to_int = int(y) # 3
# Convert int to float
int_to_float = float(x) # 10.0
Math Library:
The math module in Python is a built-in module that provides mathematical functions and constants for performing various mathematical operations. It contains functions for trigonometric calculations, logarithmic calculations, exponential calculations, and more.
Python has a math library that provides additional mathematical functions.
import math
# Square root
sqrt_result = math.sqrt(25) # 5.0
# Trigonometric functions, logarithms, etc.
Knowledge of numeric sorts and operations is critical for mathematical and quite a few programs from simple computations to complex algorithms in fields from medical computing to truth of science.