Python Variables

What are variables and how do we use them in Python?
Author

Juma Shafara

Published

November 1, 2023

Modified

August 14, 2024

Keywords

python, python programming, python variables

Photo by DATAIDEA

Variables

  • Varibles are used to store values.
  • To create a variable, use the equal sign (=).

In the examples below, we create varibales name fruit and name and we assign them values 'mango' and 'viola' respectively

fruit = 'mango'
name = 'voila'

# printing out
print(name, ' likes ', fruit )
voila  likes  mango

Rules to consider

Before choosing a variable name, consider the following.

  • Spaces are not allowed in a variable name eg my age = 34
  • A variable name can not start with number eg 1name = 'Chris'
  • Variable names can not have special characters eg n@me = 'Comfort'
  • Are case sensitive. Ie Name and name are not the same!

Examples of good variable names

character = 'Peter Griffin'
my_favorite_character = 'Stewie Griffin' # snake case
myFavoriteCharacter = 'Meg Griffin' # camel case
MyFavoriteCharacter = 'Brian Griffin' # Pascal case

Data Types

In this section of the tutorial, you will learn the most basic data types in Python

Numbers

These are two basic types of numbers and they are called:
  • integer(numbers without decimal places)
  • floating point numbers(numbers with decimal places)
# python numbers
# Integers
age = 45
population = 45000000
# Floating point numbers
height = 1.7
weight = 147.45

Find More on Numbers Here

Strings

Strings are simply text. A string must be surrounded by single or double quotes

# Strings
name = 'Juma'
other_name = "Masaba Calvin"
statement = 'I love coding'

Find More on Strings Here

Booleans

Boolean data type can only have on fo these values: True or False

Note!

The first letter of a Boolean is in upper case.

Booleans are often the result of evaluated expressions.

Forexample, when you compare two numbers, Python evaluates the expression and returns either True or False

print(5 == 5)
print(10 > 5)
print(20 < 10)
True
True
False

These are often used in if statments.

In this example, if the value of the variable age is more than 18, the program will tell the user that they are allowed to enter

age = 19

if age > 18:
    print('You are allowed to enter.')
You are allowed to enter.

Note!

You will learn more about if statements later in the course

Checking the Type of a Boolean

We can check the data type of a Boolean variable using the type() method.

married = True
print(type(married))
<class 'bool'>

Lists

  • A list is an ordered collection of data
  • It can contain strings, numbers or even other lists
  • Lists are written with square brackets ([])
  • The values in lists (also called elements) are separated by commas (,)
# Lists
names = ['juma', 'john', 'calvin']
print(names)
['juma', 'john', 'calvin']

Find More on Lists Here

A list can contain mixed data types

other_stuff = ['mango', True, 38]
print(other_stuff)
['mango', True, 38]

[]

Checking data types

To check the data type of an object in python, use type(object), for example, below we get the data type of the object stored in names

# Which data type
print(type(names))
list

Converting Types

Convert to String

The str() method returns the string version of a given object

# convert an integer to a string
age = 45
message = 'Peter Griffin is '+ str(age) + ' years old'
print(message)
Peter Griffin is 45 years old

Convert to Integer

The int() method returns the integer version of the given object.

# Convert floating point to integer
pi = 3.14159
print(int(pi))
3

Convert to Float

The float() method returns the floating point version of the given object.

side = 4
print(float(side))
4.0

What’s on your mind? Put it in the comments!

Back to top