UNDERSTANDING VARIABLES AND DATA TYPE IN PYTHON
As a beginner in python programming language, it is essential to understand some basic concepts of variables, it plays a very important role in most programming languages, and python is no exception. Hence in this write up we will learn the concepts of variables and data types in python. The following are the topics covered in this write up:
- What is variable ?
- Variable and types
- Naming convention in python
- Assigning values to variables
WHAT ARE VARIABLES?
In programming, variable is a memory location used to store information to be referenced in a computer program. It is a way of labeling data with a descriptive name that makes the data more understandable to another reader. Variable can also be seen as a container that holds one or more values.
VARIABLE AND TYPES
Variables can be written in different data types which includes integer, floats, Boolean and string and other data types.
Numbers
Python supports two types of numbers – integers (whole numbers) and floating point (decimals).
STRINGS
String is created by entering text between two single or double quotation marks.
BOOLEAN In Python, Boolean variables are defined by the True and False keywords. They are created by comparing values.
LIST
A list is a mutable, or changeable, ordered sequence of elements. In python a list is enclosed in square bracket with commas separating items.
TUPLES
A tuple is a collection which is unchangeable or immutable, it can have duplicate values as well. Tuples are similar to lists but they use parentheses () instead of square brackets.
DICTIONARY
The dictionary is Python’s built-in mapping type. This means that dictionaries map keys to values and these key-value pairs are a useful way to store data in Python. A dictionary is constructed with curly braces on either side.
NAMING CONVENTION IN PYTHON
In computer programming, a naming convention is a set of rules or guidelines for choosing the character sequence to be used as a variable. A variable name must obey the following rules:
• A variable name can be a word.
• A variable name must start with a letter or the underscore (_) character.
• A variable name cannot start with a number.
• Variable names are case-sensitive (country, Country, COUNTRY are different variable). Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers:
ASSIGNING VALUES TO VARIABLES
A variable is created the moment you assign a value to it. Once a value is assigned to a variable, you can refer to the value by that name. To assign a value to a variable, you use the assignment operator (=).
Note: The assignment operator (=) is different from the equality operator (==), the single (=) sign is used to assign values while the double (==) sign is used to check if two things are equal.
It is important to know that an assigned variable can be re-assigned and when this is done the old value is forgotten.
Note: To check the variable type, use the python in-built function type().
So far we have discussed variable, variable types and naming convention. I hope you find this article helpful.