Python Variables:
Before we dive deep into Python we need to learn basic concepts such as variables and data types supported by Python.
What is a Variable?
Variables are nothing but a container that stores data in the memory(RAM). Python
is a case-sensitive language (i.e) lower case letter is not equal to Upper case letter,
eg: a is not equal to A.
There are certain rules involved in initializing a variable in Python:
- Variables must start with alphabets or underscore.
- Variable names must not contain any symbols.
- Variable names must not be a keyword.
Syntax:
variable_name = parameter
Example:
name = "Ram" # String
age = 23 #Integer
experience = 5.5 #float
print(f"Name:{name}\nAge:{age}\nExperience:{experience}")
Output:
Name:Ram
Age:23
Experience:5.5
Different types of data can be stored in a variable, Python supports the following data types:
- Text Type: str
- Numeric Types: int, float, complex
- Sequence Types: list, tuple, range
- Mapping Type: dict
- Set Types: set, frozenset
- Boolean Type: bool - true, false
- Binary Types: bytes, byte array, memory view
- None Type: NoneType
We will see some of the data types in upcoming posts.
type():
the type() function is used to find the data type of a variable.
Code:
x = "Hello World!"
print(type(x))
Output:
class 'str'
id():
the id() function is used to get the address of the variable stored in the memory.
Code:
x = "Hello World!"
print(id(x))
Output:
2683118043376
Tags:
Python