Python Variables

 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.
In Python, we follow the camel case for naming the variable, for example, student_name.

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:

  1. Text Type: str
  2. Numeric Types: int, float, complex
  3. Sequence Types: list, tuple, range
  4. Mapping Type: dict
  5. Set Types: set, frozenset
  6. Boolean Type: bool - true, false
  7. Binary Types: bytes, byte array, memory view
  8. 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():

Python Variables - teamcoder01


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
teamcoderadmin

Post a Comment

Previous Post Next Post