Variables and data types

Basic data types

This chapter will introduce you to the basics of Python variables and data types.

The following four data types are the most basic in Python:

Data typeDescriptionExample

int

Integer

10,-5

float

Floating-point number

3.14, 2.718

str

String

'Hello World!'

bool

Boolean

True, False

Function print() is a built-in function in Python that is used to print the specified message to the screen. The following examples show how to use the print() function.

print('Hello, World!')
print(10)
print(3.14 + 2.718)
print(10 > 5)

To show the data type of a variable, use the type() function.

print(type(10))
print(type(3.14))
print(type('Hello, World!'))
print(type(True))

Cast a variable to a different data type using the following functions:

print(int(3.14))
print(float(10))
print(str(10))

Variables

Variables are used to store data values. A variable is created when a value is assigned to it.

x = 10
y = 3.14
z = 'Hello, World!'
a = True
print(x)
print(y)
print(z)
print(a)

Operators

Arithmetic operators

Operators are used to perform operations on variables and values. The following operators are available in Python:

OperatorDescriptionExample

+

Addition

x + y

-

Subtraction

x - y

*

multiplication

x * y

/

Division

x / y

%

Modulus

x % y

**

Exponentiation

x ** y

//

Floor Division

x // y

x = 10
y = 3
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
print(x // y)

Assignment operators

Assignment operators are used to assign values to variables. The following assignment operators are available in Python:

OperatorExampleEquivalent

=

x = 10

+=

x += 10

x = x + 10

-=

x -= 10

x = x - 10

*=

x *= 10

x = x * 10

/=

x /= 10

x = x / 10

%=

x %= 10

x = x % 10

**=

x **= 10

x = x ** 10

//=

x //= 10

x = x // 10

x = 10
x += 5
print(x)
x -= 5
print(x)
x *= 5
print(x)

x = 10
x /= 5
print(x)
x = 10
x %= 5
print(x)
x = 10
x **= 5
print(x)
x = 10
x //= 3
print(x)

Comparison operators

Comparison operators are used to compare two values. The following comparison operators are available in Python:

OperatorDescriptionExample

==

Equal

x == y

!=

Not equal

x != y

>

Greater than

x > y

<

Less than

x < y

>=

Greater than or equal to

x >= y

<=

Less than or equal to

x <= y

x = 10
y = 5
print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)

Logical operators

Logical operators are used to combine conditional statements. The following logical operators are available in Python:

OperatorDescriptionExample

and

Returns True if both statements are true

x > 5 and x < 10

or

Returns True if one of the statements is true

x > 5 or x < 10

not

Reverse the result

not(x > 5 and x < 10)

x = 10
print(x > 5 and x < 15)
print(x > 5 or x < 15)
print(not(x > 5 and x < 15))

Comments

Comments are used to explain the code and make it more readable. Comments start with a # and Python will ignore them.

# This is a comment
print('Hello, World!') # This is also a comment

A multi-line comment can be created using triple quotes.

'''
This is a multi-line comment
This is a multi-line comment
This is a multi-line comment
'''
print('Hello, World!')

Last updated