Programming in Python 1 – COMING SOON
About Lesson

1.4 Data Types


In computer science, a data type refers to the classification or categorisation of data items. It signifies the type of value a variable can hold and the operations that can be performed on that value.

Here are the standard data types that exist in Python.

 Integer  – int()

Integer, or int in Python, represent whole numbers, both positive and negative. They have no fractional part and are used for counting or measuring items in programming. Here is an example:

age = 23
number_of_books = 5
number_difference = -7

 

Floating Point Number – float()

Floating point numbers, or float, refer to numbers that have a decimal point. They represent real numbers, and Python automatically identifies any number with a decimal point as a float. For example:

pi = 3.14
height = 1.75
price = 48.5

 

String – str()

A string or str is a collection of characters. We encountered strings before when we discussed output using the print() statement.  Strings are essential in Python; several operations are defined on them, so lesson 4 is dedicated to strings.

Strings are enclosed in single, double and triple quotation marks. Here are some examples of strings.

name = "John"
message = 'Welcome to Python programming!'
city = '''London is my favourite city'''

 

Boolean – bool()

A Boolean or bool data type can have one of two true or false values. Boolean are often used in conditional statements; they allow for logical comparison such as equality, greater than, less than, etc. Here are some examples of Boolean.

is_raining = True
is_sunny = False
greater_than = 59 > 10

These are the four built-in data types in Python. They are essential when dealing with variables, as the data type of a variable determines the range of values that can be stored in that variable and its possible operations.

Operations defined on numeric data type

The normal mathematical operations are defined on the numeric data type (int, float).  The program below shows the normal mathematical operations and give the result as a comment.

val1 = 10
val2 = 2

#Addition
print(val1 + val2)    # equals 12

#Subtraction
print(val1 - val2)    # equals 8

#Multiplication
print(val1 * val2)    # equals 20

#Division
print(val1/val2)      # equals 5.0

#Integer division
print(val1 // val2)  # equals 5

#Modulo
print(val1 % val2)  # equals 0

#Power
print(val1 ** val2)  # equals 100

Integer division : returns the integer part of a division therefore 7 // 2 == 3  since 2 goes into 7;  3 times.

Modulo: Returns the remainder after integer dividing a number by the other.  Therefore 23 % 5 == 3 since 23 divides by 5 is 4 with a remainder of 3.

Power: Double asterisk (star) are used to raised the first number to the second number therfore 5 **2 == 25 as 5 squared is equalt to 25.

0% Complete
Scroll to Top