Python basics
  • Tutorial
  • Exercices
  • About
  1. Fundamentals
  2. 3-Numbers
  • Introduction
    • Python
  • Fundamentals
    • 0-Basic Syntax
    • 1-Variables
    • 2-Data types
    • 3-Numbers
    • 4-Strings
    • 5-Lists
    • 6-Booleans
    • 7-Loops
    • 8-Functions
    • 9-Dictionary
  • Advanced
    • 10-Numpy
    • 11-Matplotlib
    • 12-DataFrame
  • CLI
    • Exercices
    • Solutions
  • Git
    • Git introduction
    • Git branch
    • Git exercices
  • SQL
    • SQL Exercices
    • SQL Solutions

On this page

  • Types
    • Integers
    • Float
    • Complex
  • Numerical operations
  • Assignment operators
  1. Fundamentals
  2. 3-Numbers

3-Numbers

Notebook

Link to the Google Colaboratory notebook: 3-Numbers

There are three different types of native numbers: int, float and complex.

Types

Integers

An integer int is a positive or negative number without decimals.

x = 1
y = 35656225487711
z = -825552

print(type(x), type(y), type(z))
<class 'int'> <class 'int'> <class 'int'>

Float

A float is positive or negative number containing one or more decimals.

It can also be a scientific number, specified by the e that indicates a power of 10.

x = 1.1934
y = -82.55
z = 2.3e-5


print(type(x), type(y), type(z))
<class 'float'> <class 'float'> <class 'float'>

Complex

A complex number is written with the j as the imaginary part.

x = 1+3j
y = 10j
print(type(x), type(y))
<class 'complex'> <class 'complex'>

Numerical operations

Python arithmetic operators are used to perform mathematical operations on numerical values.

Operator Name Example
+ Addition 10 + 20 = 30
- Subtraction 20 – 10 = 10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 22 % 10 = 2
** Exponent 4**2 = 16
// Floor Division 9//2 = 4
x, y = 100, 5

print("Addition", x+y)
print("Subtraction", x-y)
print("Multiplication", x*y)
print("Division", x/y)
print("Modulus", x%y)
print("Exponent", x**y)
print("Floor division", x//y)
Addition 105
Subtraction 95
Multiplication 500
Division 20.0
Modulus 0
Exponent 10000000000
Floor division 20

Assignment operators

Python assignment operators are used to assign values to variables.

Operator Name Example
= Assignment Operator a = 10
+= Addition Assignment a += 5 (Same as a = a + 5)
-= Subtraction Assignment a -= 5 (Same as a = a - 5)
*= Multiplication Assignment a *= 5 (Same as a = a * 5)
/= Division Assignment a /= 5 (Same as a = a / 5)
%= Remainder Assignment a %= 5 (Same as a = a % 5)
**= Exponent Assignment a **= 2 (Same as a = a ** 2)
//= Floor Division Assignment a //= 3 (Same as a = a // 3)
# Assignment Operator
a = 10

# Addition Assignment
a += 5
print ("a += 5 : ", a)

# Subtraction Assignment
a -= 5
print ("a -= 5 : ", a)

# Multiplication Assignment
a *= 5
print ("a *= 5 : ", a)

# Division Assignment
a /= 5
print ("a /= 5 : ",a)

# Remainder Assignment
a %= 3
print ("a %= 3 : ", a)

# Exponent Assignment
a **= 2
print ("a **= 2 : ", a)

# Floor Division Assignment
a //= 3
print ("a //= 3 : ", a)
a += 5 :  15
a -= 5 :  10
a *= 5 :  50
a /= 5 :  10.0
a %= 3 :  1.0
a **= 2 :  1.0
a //= 3 :  0.0
2-Data types
4-Strings
 

Copyright 2023, Clément Bernard