Variables are a fundamental concept in programming and are used extensively in Python to store and manipulate data.
In Python, variables are used to store values. A variable is created by assigning a value to it using the “=” operator.
Once a variable is created, it can be used in other parts of the program to perform calculations, comparisons, or other operations.
Python is a dynamically typed language, which means that you don’t need to specify the type of a variable when you create it.
The type of a variable is determined automatically based on the value it is assigned.
Variable names in Python can consist of letters, numbers, and underscores, but must start with a letter or underscore.
Python is case-sensitive, which means that “x” and “X” are considered two different variables.
Here is an example of how to create and use variables in Python:
# Assign a value to a variable
x = 10
# Use the variable in a calculation
y = x + 5
# Print the value of the variable
print(y)
In this example, the variable “x” is assigned the value 10, and the variable “y” is assigned the result of adding 5 to “x”. The value of “y” is then printed to the console.
Basics DataTypes in Python:
Python has several built-in data types that are fundamental to the language. Here are some most commonly used data types which are described below with its examples:
Integers: Integers:
These are whole numbers (positive, negative, or zero) that can be used in arithmetic operations.
x = 42
print(x)
# Output: 42
Floats:
Floats: These are decimal numbers that can also be used in arithmetic operations.
y = 3.14
print(y)
# Output: 3.14
Strings:
Strings: These are sequences of characters that can be used to represent text.
name = "Alice"
print("Hello, " + name)
# Output: Hello, Alice
Booleans:
Booleans: These are binary values (True or False) that are used in logical operations.
a = True
b = False
print(a and b)
# Output: False
Lists:
Lists: These are ordered collections of elements that can be of any data type.
fruits = ["apple", "banana", "cherry"]
print(fruits[1])
# Output: banana
Tuples:
Tuples: These are similar to lists, but are immutable (cannot be changed once created).
person = ("Alice", 25)
print(person[0])
# Output: Alice
Dictionaries:
Dictionaries: These are unordered collections of key-value pairs that can be used to represent structured data.
person = {"name": "Alice", "age": 25}
print(person["age"])
# Output: 25
Sets:
Sets: These are unordered collections of unique elements (In sets we start from 0).
numbers = {1, 2, 3, 3, 4, 5}
print(len(numbers))
# Output: 5
(Here the output is 5 according to (0,1,2,3,4,5) which indicates each element in “numbers”)
Python also allows for the creation of custom data types through classes and objects.
Understanding these basic data types is essential to working with data in Python.
By combining these data types and using them in various operations and functions, you can perform a wide range of tasks in Python.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.