Table of Contents

Python is not statically typed, but completed object oriented programming language when creating variables in Python. This means that one will not need to declare variables before using or declare the type of variable (string, number, integer). There are a few basic types of variables that will be discussed in this Python tutorial.

## Variables and Types in Python

Variables are stored as a memory location where value is stored and are created when first assigned. Before executing a variable to return a value, the variable must be assigned before referenced. The Python interpreter allocates memory on the basis of the variable data types (string, integer, number).

## Python Variable Naming Rules

Python variables must begin with a letter (lower case or upper case) or an underscore ( _ ). The following characters after the initial (first character) can be any form letters, numbers or underscore at a reasonable length. Python has reserved words that are not allowed to be used as a variable.

### Strings

Strings are defined by using single quotes ( ‘ ‘ ) or double quotes ( ” ” ) in Python. Double quotes is more commonly used because it allows apostrophes to be used where as single quotes would terminate the string variable. Below is an example using single quotes and an apostrophe to print out a string variable.

**Input:**

1 2 3 | sample_string = 'Don't forget to study Python!' print("This will not work with an apostrophe: {sample_string} ") |

**Output:**

1 | SyntaxError: invalid syntax |

Let’s try the below with double quotes and an apostrophe:

**Input:**

1 2 3 | sample_string = "Don't forget to study Python!" print(f"This will not work with an apostrophe: {sample_string} ") |

**Output:**

1 | This will not work with an apostrophe: Don't forget to study Python! |

### Numbers

There are two types of numbers that Python supports: floating numbers and integers. As shown below, a variable is created for integer 7 and another variable is used the convert the integer to a flat by using the Python float() function.

**Input:**

1 2 3 4 5 | sample_integer = 7 sample_float = float(sample_integer) print(f"This is a sample Python integer: {sample_integer}") print(f"This is a sample Python floating number: {sample_float}") |

**Output:**

1 2 | This is a sample Python integer: 7 This is a sample Python floating number: 7.0 |

## Numbers and Simple Operators

Python allows using arithmetic operators to create variables. For example, one can create variable for addition, subtraction, multiplication , and division.

**Input:**

1 2 3 4 5 6 7 8 9 10 11 12 13 | num1 = 20 num2 = 10 addition = num1 + num2 subtraction = num1 - num2 multiplication = num1 * num2 division = num1 / num2 print(f"First variable: {num1} ") print(f"Second variable: {num2} ") print(f"Python addition example: {addition} ") print(f"Python subtraction example: {subtraction} ") print(f"Python multiplication example: {multiplication} ") print(f"Python division example: {division} ") |

**Output:**

1 2 3 4 5 6 | First variable: 20 Second variable: 10 Python addition example: 30 Python subtraction example: 10 Python multiplication example: 200 Python division example: 2.0 |