Variables in Python

Variables in Python: 

In Python, variables are used to store data values. Variables are essentially names that refer to a particular location in memory where a value is stored. In the context of data storage, variables can be viewed as containers.

Python Variables

Variable in Python

In Python, variables are used as a container and when we assign a value, a variable is created.

There are some rules of Python variable:

  • Variable names must begin with either an underscore (_) or a letter (a-z, A-Z).
  • The remaining characters in the variable name can be letters, numbers (0–9), and underscores.
  • MyVar and myvar are different variables because variable names are case-sensitive.
  • Some reserved keywords in Python (including if, while, and for) cannot be used as variable names.

Tasks done in Python Variable:

Assigning a Value:

For Assign a value we use single equals sign (=).

Example: x = 32

#Here ‘x’ is a variable and it contain the value ’32’.

Casting:

Casting is a technique that can be used to specify the data type of a variable.

Example:

  • x = float(3) #x will be 3.0
  • y = str(3) #y will be ‘3’

Get the Type:

The type() method provides the data type of a variable.

Example:

  • print(type(3)) #int
  • print(type(“Prepinsta”)) #str

Case Sensitive:

Variables are case sensitive.

Example: 

X = 30
x = 3
#x will not overwrite 

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription