DEV Community

Zakir Hussain Parrey
Zakir Hussain Parrey

Posted on

Variables and Data Types in Python: A Beginner's Guide

Python is an easy-to-learn, powerful programming language. One of the fundamental concepts every beginner should master is how variables work and the different data types available in Python.

What Are Variables?

A variable stores data that your program can use, modify, or display. In Python, you don't have to declare the type of a variable explicitly. Python figures it out when you assign a value.

# Assigning values
name = "Alice"
age = 25
temperature = 36.6
Enter fullscreen mode Exit fullscreen mode

Common Data Types in Python

  1. String (str) Text data. Enclosed in single or double quotes.
   greeting = "Hello, world!"
Enter fullscreen mode Exit fullscreen mode
  1. Integer (int) Whole numbers.
   count = 42
Enter fullscreen mode Exit fullscreen mode
  1. Float (float) Numbers with decimals.
   pi = 3.14159
Enter fullscreen mode Exit fullscreen mode
  1. Boolean (bool) Logical values: True or False.
   is_active = True
Enter fullscreen mode Exit fullscreen mode
  1. List Ordered, changeable collection of items.
   fruits = ["apple", "banana", "cherry"]
Enter fullscreen mode Exit fullscreen mode
  1. Dictionary (dict) Key-value pairs.
   student = {"name": "Bob", "age": 20}
Enter fullscreen mode Exit fullscreen mode

Dynamic Typing

Python lets you change the type of a variable any time:

x = 5      # x is initially an integer
x = "five" # now x is a string
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding variables and data types is essential for writing effective Python programs. Practice by creating variables of different types and experimenting with them!

Top comments (0)