Integer
Integers
Integers are whole numbers that can be positive, negative, or zero, such as -3, 0, 5, etc.
Storing Integers in Python
In Python, you can store integers in variables just like any other data type. For example:
num1 = 10 num2 = -5 num3 = 0
Working with Integer Variables
You can perform operations directly on integer variables and store the results in a new variable. For example:
result = num1 + num2 print(result) # Output: 5
Python integer operations
Addition:
Python provides the + operator to add two integers. For example:
print(3 + 5)
Subtraction:
The - operator is used for subtracting one integer from another. For example:
print(10 - 6)
Multiplication:
For multiplication, Python uses the * operator. For example:
x = 4 * 5
print(x)
Division:
Python has two division operators. The / operator performs normal division and returns a float. For example:
print(10 / 3) # outputs a floating point value 3.3333
The // operator performs floor division and returns the quotient as an integer. For example:
print(10 // 3) # Outputs a integer value 3
Modulo:
The % operator is used for finding the remainder of division between two integers. For example:
remainder = 10 % 3
print(remainder) #outputs 1
Modulo is used widely within computer science, in everything from encryption to library book verification.
In Python, what is the result of the expression 7 * 4?
Converting between Integers and Strings
Sometimes, you may need to convert an integer into a string or vice versa. Python allows you to easily convert between these data types using the str() and int() functions. For example, to convert the integer 10 into a string, you can use the str(10) function, which will give you the string "10".
x = 6
x = str(x) # x is now '6', not 6
print(x*2) # Outputs '66', not 12
When you convert a number to a string the mathematical operators no longer work like before.
'5' + '4' # '54'
'4' * 3 # 444
'5' + 4 #Error
6 / '2' #Error
In Python, what is the result of the expression '5' * 4?
What is the result of the expression 42 + '18' in JavaScript?
Review: Fill in the Blanks
When working with integer variables, you can perform mathematical operations and store the results in a new . For instance, using the addition operator, you can add two integers, such as in the expression = num1 + num2, which would output 5 when num1 is 10 and num2 is -5.
Python provides several operators for performing basic arithmetic operations. The operator is used for addition, while the operator is utilized for subtraction. To multiply integers, the operator is employed, and for division, Python has two operators: the operator for normal division and the operator for floor division, which returns the quotient as an integer.
Converting between integers and strings is also possible in Python using the and functions. For example, converting the integer 10 into a string can be done with str(10), resulting in the string "10". Itβs important to note that when a number is converted to a string, the mathematical may not function as expected, leading to errors when attempting operations between a string and an integer.
Complete! Ready to test your knowledge?
Integers
- Integers
- Python integer operations
- Converting between Integers and Strings