popolo - code simple..

popolo - code simple..

Python Basics - Variables and Data Types

Python Basics - Variables and Data Types

Theodoros Kafantaris's photo
Theodoros Kafantaris
·Apr 22, 2022·

4 min read

Table of contents

  • Variables
  • Variable rules
  • Standard Data Types


Variables are containers for storing data values.

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

>>> message = "Hello Python!"
>>> print(message)
Hello Python!

We’ve added a variable named message. Every variable is connected to a value, which is the information associated with that variable. In this case the value is the "Hello Python!" text.

Variable rules

We must follow some rules and guidelines when we are creating variables.

  • Variable names can contain only letters, numbers, and underscores. They can start with a letter or an underscore, but not with a number. For instance, we can call a variable post_1 but not 1_post.

  • Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, welcome_mail works, but welcome mail will cause errors.

  • Avoid using Python keywords and function names as variable names; that is, do not use words that Python has reserved for a particular programmatic purpose, such as the word print.

  • Variable names should be short but descriptive. For example, name is better than n, student_name is better than s_n, and name_length is better than length_of_persons_name.

  • Be careful when using the lowercase letter l and the uppercase letter O because they could be confused with the numbers 1 and 0.

  • Variable names are case-sensitive. So variable name is different from variable Name.

When an error occurs in our program, the Python interpreter does its best to help us figure out where the problem is. The interpreter provides a traceback when a program cannot run successfully. A traceback is a record of where the interpreter ran into trouble when trying to execute your code.

Standard Data Types

The data stored in variable can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

Python has five standard data types:

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Python Numbers

Number data types store numeric values. Number objects are created when we assign a value to them.

variable1 = 1
variable2 = 2

Python supports four different numerical types:

  • int (signed integers) (ie. 10, 20, 30)
  • long (long integers, they can also be represented in octal and hexadecimal) (ie. 72324361L)
  • float (floating point real values) (ie. 7.20)
  • complex (complex numbers) (ie. 3.14j)

Python Strings

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes.

>>> text = 'Hello Python!'
>>> print(text)     # Prints complete string
Hello Python!

Python Lists

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets []. To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
another_list = [123, 'john']

Python Tuples

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

The main differences between lists and tuples are: Lists are enclosed in brackets [ ] and their elements and size can be changed, while tuples are enclosed in parentheses ( ) and cannot be updated. Tuples can be thought of as read-only lists.

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
another_tuple = (123, 'john')

Python Dictionary

Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in php and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

Dictionaries are enclosed by curly braces { } and values can be assigned and accessed using square braces [].

dict = {}
dict['name'] = "Joe"
dict[2]     = "apple"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
Share this