Introduction
In this tutorial, we will dive into the basics of Python programming by understanding variables and data types. This is a crucial step for any beginner, as it forms the foundation for all future coding endeavors. Let’s get started!
1. What is a Variable?
A variable in Python is used to store data values. It’s like a container that holds information which can be referenced and manipulated in your code. Variables can store different types of data, and Python determines the type based on the value assigned.
Example:
# Assigning values to variables
name = "Alice"
age = 25
is_student = True
# Printing variable values
print(name)
print(age)
print(is_student)
2. Data Types in Python
Python has various data types to represent different kinds of data. The most common data types are:
- Integers: Whole numbers without a decimal point.
- Floats: Numbers with a decimal point.
- Strings: Sequence of characters enclosed in quotes.
- Booleans: Represents True or False values.
Example:
# Integer
num = 10
print(type(num)) # Output: <class 'int'>
# Float
pi = 3.14
print(type(pi)) # Output: <class 'float'>
# String
greeting = "Hello, World!"
print(type(greeting)) # Output: <class 'str'>
# Boolean
is_sunny = False
print(type(is_sunny)) # Output: <class 'bool'>
Practice Time!
Try creating your own variables and assigning them different types of data. Use the print()
function to display their values and types.