DEV Community

Chandru
Chandru

Posted on

Introduction to Python: Variables and Data Types

Python is one of the most popular programming languages in the world. It is a high-level, general-purpose programming language created by Guido van Rossum and first released in 1991.

Python is known for its simple syntax and readability, making it a great choice for beginners who are starting their programming journey.

Unlike some other programming languages, Python allows us to write code using fewer lines, which makes it easier to learn and understand.

Python is widely used in web development, artificial intelligence, machine learning, data science, automation, and cybersecurity.

Variables in Python

Let's start with one of the basic concepts of Python: Variables.

A variable is simply a name used to store data in a program. We can create variables and assign values to them without specifying their data types.

For example:

name = "Chandru"
age = 22
language = "Python"

print(name)
print(age)
print(language)
Enter fullscreen mode Exit fullscreen mode

Output:

Chandru
22
Python
Enter fullscreen mode Exit fullscreen mode

In the above example, name, age, and language are variables that store different values.

One interesting thing about Python is that we don't need to declare the variable type explicitly. Python automatically identifies the type based on the assigned value.

Data Types in Python

Every value in Python has a data type. A data type tells us what kind of value a variable holds.

Python provides several built-in data types.

Let's look at some commonly used ones:

1. Integer (int)

Integer represents whole numbers without decimal points.

age = 22
marks = 95

print(age)
print(marks)
Enter fullscreen mode Exit fullscreen mode

2. Float (float)

Float represents numbers that contain decimal points.

price = 99.99
height = 5.8

print(price)
print(height)
Enter fullscreen mode Exit fullscreen mode

3. String (str)

String is used to store text. We can create strings using single or double quotation marks.

name = "Chandru"
language = 'Python'

print(name)
print(language)
Enter fullscreen mode Exit fullscreen mode

4. Boolean (bool)

Boolean represents only two values: True or False.

is_learning = True
is_completed = False

print(is_learning)
print(is_completed)
Enter fullscreen mode Exit fullscreen mode

5. List (list)

A list is used to store multiple values in a single variable. Lists are ordered and mutable, meaning we can modify their elements.

languages = ["Python", "Java", "C"]

print(languages)
Enter fullscreen mode Exit fullscreen mode

6. Tuple (tuple)

A tuple is similar to a list, but its elements cannot be modified after creation.

numbers = (10, 20, 30)

print(numbers)
Enter fullscreen mode Exit fullscreen mode

7. Set (set)

A set is used to store unique values. It does not allow duplicate elements.

numbers = {10, 20, 30, 10}

print(numbers)
Enter fullscreen mode Exit fullscreen mode

8. Dictionary (dict)

A dictionary stores data in key-value pairs.

student = {
    "name": "Chandru",
    "age": 22,
    "language": "Python"
}

print(student)
Enter fullscreen mode Exit fullscreen mode

Checking Data Types

Python provides a built-in function called type() to identify the data type of a variable.

For example:

name = "Chandru"
age = 22
price = 99.99
is_learning = True

print(type(name))
print(type(age))
print(type(price))
print(type(is_learning))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)