Posted on 6/18/2025 5:27:38 PM by Admin

Python Basics: Variables and Data Types

In the previous article, you got an introduction to Python and learned how to get started with it. Hopefully, you have Python 3.x and PyCharm Community Edition installed in your system. If not, go back and get it installed so you can practice the code examples discussed in this article.

You already know how to get Python to say something to the user. But what if you want it to remember something? That's where the variables and data types come into play. In this article, you will learn about the variables that act as a container for information and the data types that help identify the type of information stored in the variables. Mastering these core concepts is highly crucial and is almost the same in all programming languages. So, if you know data types and variables in Python, you know it in other programming languages as well.


Variables: Containers of Data

Variables are the named memory locations that are used to store data temporarily during program execution. Their value vanishes as soon as the program execution is completed. The term 'variable' is derived from two words, i.e., 'Vary' means 'Change' and 'Ables' means 'Capable of.' So, combinedly, the term 'Variable' means something that has the capability to change. The value of the variables can be changed anywhere in the program.

In Python, creating and using variables is pretty straightforward.

Syntax:


variable_name = value

Example:


number = 100  #Assigns 100 to the variable "number".

name = "John Smith" #Assigns "John Smith" to the variable "name".

percentage = 11.5  #Assigns 11.5 to the variable "percentage".

Variable Naming Conventions

Although you can name your variables as you want, the following are some naming conventions that are essential to follow:

  • A variable name must start with a letter or an underscore (_) sign.
  • Example: name, _percentage, Age.

  • A variable name must not start with a number. For instance, 1stNumber is invalid. Instead, you can use first_number as the variable name.
  • Variable names can contain letters, numbers, or underscores. No other special characters (including spaces) are allowed.
  • Example: First Name (Invalid), First_Name (valid).

  • Variable names are case-sensitive. For example, 'name' is different from 'Name'.
  • You cannot use reserved words as variable names.
  • Example: 'print' is an invalid variable name since it is a reserved word.

Reserved Words are the keywords that are part of the Python syntax.

While following the above conventions, it is best to keep your variable names descriptive. For example, instead of number = 10, use age = 10. This makes your code more readable and meaningful. The Python community uses the snake_case to name variables and functions. It contains the words in lowercase, separated by underscore (_). For example, total_score, first_number, user_first_name, etc.


Data Types in Python

The data types are used to determine the type of information being stored in a variable. Python uses dynamic data types, meaning that you do not need to specify the data type in advance. Python determines it automatically based on the value assigned to the variable. However, being aware of different data types comes in handy in various situations. So, the following are some core Python data types:

I. Numbers

These data types are used to perform calculations and store numeric data.

i. Integers (int):

These are the whole numbers without a decimal point.


student_age = 15
length = 10

ii. Floating-point Numbers (float):

These are the positive or negative numbers with a decimal point.


percentage = 10.4
temperature = -0.5

II. Strings

Strings are a collection of characters. These are highly useful for messages, sentences, and names. They are usually enclosed in single or double quotes, and for multiline strings, you can use triple quotes as well.


first_string = 'This is the string with single quotes'

second_string = "This is the string with double quotes. It works the same as the string with a single quote."

multi_line_String = """I am the multiline string.

I can have as many lines as you want.

I come in handy when you need to display long messages."""

III. Booleans

The Boolean data type is used to manipulate conditions in programming. It has only 2 values, i.e., True and False. Please note that a Boolean value always starts in an uppercase letter.


has_passed = True
is_adult = False


Bonus: Comments in Python

In any programming language, comments are the lines of code that are not executed and are ignored by the compiler or interpreter on run time. They come in handy while debugging the code, remembering what a block of code does, or making your code more readable for other programmers.

In Python, we specify comments with the # symbol.


# This is a Comment.

The comments can be single-line or multiline. There's no separate syntax for specifying multiline comments in Python, though. However, we can use the multiline strings as multiline comments as follows:


# A multiline comment

# can be specified

#like this, or

"""A Multiline comment

can be specified

like this."""

Adding comments to the code is considered a good practice. However, you should make sure that your comments are concise and meaningful and explain the complex logic in your code.

So this is it with variables and data types. Hopefully, you now understand how to declare variables and the different types of data you can store in them. In the next article, we will explore some basic operations and learn how to manipulate data.


Sharpen Your Skills with These Next Guides