Python Part 2: Basic Data Types and Variables in Python


Please Subscribe Youtube| Like Facebook | Follow Twitter

Basic Data Types and Variables in Python

Python is a popular programming language used in data science, web development, and artificial intelligence. In this article, we will discuss basic data types and variables in Python. Understanding data types and variables is crucial to working with Python as they are the building blocks of any program.

Data Types in Python

In Python, there are several built-in data types that are used to store different kinds of information. The following are the most commonly used data types in Python:

  1. Numeric Data Types: Numeric data types include integers, floats, and complex numbers. Integers are whole numbers, while floats are decimal numbers. Complex numbers are made up of a real and imaginary part.
  2. Boolean Data Type: Boolean data type can only take two values: True or False.
  3. String Data Type: String data type is used to store a sequence of characters.
  4. List Data Type: List data type is used to store a collection of items. The items can be of any data type.
  5. Tuple Data Type: Tuple data type is similar to list data type, but the values stored in a tuple cannot be changed once they are assigned.
  6. Dictionary Data Type: Dictionary data type is used to store key-value pairs.

Variables in Python

In Python, a variable is used to store a value. A variable is created when a value is assigned to it. Python variables are dynamic, which means they can be reassigned with a new value of a different data type.

Declaring Variables in Python

In Python, a variable is declared by assigning a value to it. For example, the following code declares a variable named x and assigns the value 10 to it:

x = 10

Python Variable Naming Convention

When naming variables in Python, there are a few rules to follow:

  1. A variable name must start with a letter or underscore character.
  2. A variable name cannot start with a number.
  3. A variable name can only contain alphanumeric characters and underscore character.
  4. Variable names are case-sensitive.

Here is an example of a variable declaration that follows these conventions:

first_name = "John"
last_name = "Doe"
age = 25

Python Output Function

Python has a built-in function called print() that is used to display output to the console. Here is an example of using the print() function to display the value of a variable:

x = 10



Example Code

# Integer data type
a = 42
print("Value of a:", a)
print("Data type of a:", type(a))

# Float data type
b = 3.14
print("Value of b:", b)
print("Data type of b:", type(b))

# Boolean data type
c = True
print("Value of c:", c)
print("Data type of c:", type(c))

# String data type
d = "Hello, World!"
print("Value of d:", d)
print("Data type of d:", type(d))

# List data type
e = [1, 2, 3, 4, 5]
print("Value of e:", e)
print("Data type of e:", type(e))

# Tuple data type
f = (6, 7, 8, 9, 10)
print("Value of f:", f)
print("Data type of f:", type(f))

# Dictionary data type
g = {"name": "John", "age": 30, "gender": "male"}
print("Value of g:", g)
print("Data type of g:", type(g))


Value of a: 42
Data type of a: <class 'int'>
Value of b: 3.14
Data type of b: <class 'float'>
Value of c: True
Data type of c: <class 'bool'>
Value of d: Hello, World!
Data type of d: <class 'str'>
Value of e: [1, 2, 3, 4, 5]
Data type of e: <class 'list'>
Value of f: (6, 7, 8, 9, 10)
Data type of f: <class 'tuple'>
Value of g: {'name': 'John', 'age': 30, 'gender': 'male'}
Data type of g: <class 'dict'>

In this example, we declare several variables with different data types. We then use the print() function to display the values and types of these variables.

Now Below script demonstrates the dynamic nature of Python variables, as we can change the values of these variables at any time. For example, we can change age to a string data type:

Change variable data type

age = "25"
print("Age:", age)


Age: 25

Note that when we changed age to a string data type, the print() function outputs the value as a string even though it was originally declared as an integer.

Overall, understanding data types and variables is an essential concept in Python programming. By learning how to use and manipulate different data types and variables, you can create more complex and powerful programs.

Python Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter

Leave a Reply

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