Variables and data types in Python

In this tutorial, you will learn about the concept behind variable names or naming in computer programming and how to work with different data types in Python.

What is a variable?

A variable is a symbolic name used to identify areas of storage in which data is stored in the computer. They are used to associate a value or object with a name.

In Python, unlike some other programming languages, a variable is not tied down to a given type.

They are not declared to have a type, as they are in many other languages; they are simply assigned a value.


x = 5

In the above expression, the number or value 5, is assigned to a variable with the name x.

Also, you can assign the variable x to as many values as you wish. However, the last assignment becomes the current value associated with the variable.


x = 'hello'

print(x)

#output
#hello

x = [1, 2, 3]

print(x)
#output
#[1,2,3]

Data Types in Python

Every programming language has its own data types including Python. Data types in Python are ways in which data are classified based on the kind of operations to be performed on them.

For example, the number 2 and the word ‘hello’ are both data, but different with respect to the kind of operations that can be performed on them.

You can perform mathematical additions on numbers, but you cannot do the same to a piece of text.

Data can be categorized in various ways in Python, but essentially into the following types:

  • Numbers
  • Strings
  • Boolean

Numbers

Numbers, also known as the numerical types consist of integers, floats and complex numbers.

Integers are numbers without any fractional parts or decimal points. They may be positive or negative whole numbers. Numbers like 1, 4, 100, 9999, and -2 are integers since they are whole numbers.

Floats are numbers with fractional parts or decimal points. Just like integers, they can be positive or negative numbers. Hence, numbers such as 0.01 and -100.5 are floats.

Complex numbers, as in mathematics are numbers with real and imaginary numbers. A number like 1 + 3j is a complex number, with 1 the real number and 3 the imaginary number.

Regardless of whether a number is an integer or float, you can perform operations such as addition, subtraction, division and multiplication on all numbers.

Strings

Strings are sequences or series of characters enclosed within the double or single quotes. It is a collection of characters like alphabets, numbers and signs. They are used to represent textual information, such as name, gender, colour, address, phone number and so on.

To create a string in Python, you simply enclose characters into matching quotation signs.


name = 'hello'

If you are using the single quotation sign, the opening and closing signs must match. In the example, the opening and closing quotation signs must be single.


name = 'hello' #correct

name = "hello' #correct

name = 'hello" #wrong

However, if you are using the double quotation sign, the opening and closing signs must be double. If you fail to use matching signs, then you will get an error.

Here are some of the operations you can perform on strings.

Concatenation

Joining or combining two or more strings together to get another string is known as concatenation. To combine strings, you use the plus (+) operator.

For instance:


first = 'John'

last = "kelly'

fullname = first + last

print(fullname)

#output

#JohnKelly

The output from the above expression is JohnKelly. However, if you want to have a space separating the two names, John and Kelly, you have to include a space character in between the first and last as shown below:


fullname = first + ' ' + last

print(fullname)

#output

#John Kelly

Built-in methods for strings

There are so many built-in methods for strings in Python. These methods enable you to create and manipulate strings like changing the cases of strings to lower or upper cases.


print('john'.upper())

#output

#JOHN

print('John'.lower())

#output

#john

Booleans

The boolean data type can only contain two values, True or False. It has only two possible values, which are False and True or 0 and 1.

Rules for naming variables

These are some of the conventions or rules to keep in mind when naming variables.

1. Variables can contain letters, numbers and symbols

You can use alphabets (both upper and lower), numbers and some symbols in your variable names. Symbols like #, $, *, @, #, !, ^, (,  and ) have special meanings in Python and cannot be contained invalid variable names. However, you are safe to you underscore (_) and hyphen ().

Below are examples of valid variable names in Python.

  • Car
  • name123
  • full_name
  • jghdkp9e
  • building-height

The following are invalid names since they contain characters with special meanings in Python.

  • car^
  • jk#

2. The first character of a variable cannot be a number

The first character of a variable name can be an alphabet or underscore, but not a number or hyphen. Below are examples of invalid variable names in Python.

  • 123name
  • -number

3. Avoid the use of keywords or reserved words for variable names

Reserved words or keywords are words that have special meanings in a programming language. Below are Python’s keywords.

reserved keywords in python

4. Variables should be short and descriptive

When choosing your variable names, you should ensure that you use names that are short and meaningful. Avoid using single characters like a, b, x, y and so on. Rather, use names that are related to the value or object you wish to associate the variable name to.

For example:

  • fullname for full name
  • avg for average
  • pg for pages

5. Variable names are case-sensitive

You should also keep in mind that names in Python are case-sensitive. In other words, colour and Colour are two different names.

Leave a Reply

Your email address will not be published. Required fields are marked *