Python – Variable

In this tutorial, we will learn about variables in Python. we will also discuss to define a variable, assign a value to variables in Python.

What is Variable:

  • In Python, variables are a memory location.
  • It can store values like numbers and text.
  • The value of a variable can be changed.
  • The variable is always assigned with the equal(=) sign.

Example

x=10
y="Learn Python"
print(x)
print(y)

Python is a dynamically typed language, so, you need not to declare type of a variable.

Variable Assignment:

In Python, the variable assignment can be done by using equal(=) symbol. One or more values can assign in variables.

We can assign a single value to a variable.

#single value assignment in a variable
n=10
print(n)

We can assign multiple values to a single variable.

#multiple values assignment in a variable
n=10,20,30  
print(n)

And we can assign multiple values to multiple variables.

#multiple values assignment in multiple variables
a,b,c=10,20,30
print(a)
print(b)
print(c)

Rules for Naming Variables

  • The variable name can be a combination of letters, numbers, and underscore.
  • The first character of a variable name should not be a number, it must start with a character or underscore.
  • A variable name can have both uppercase and lowercase or a combination of both cases.
  • Variable name should not be a reserved keyword(predefined word in Python).
  • Name of the variable should be meaningful.

173
Created on By S. Shekhar

Python - Quiz 4

1 / 4

Which of the following is not true about variables?

2 / 4

You need not to declare a type of variable in python.

3 / 4

Which of the following is correct for a variable assignment?

4 / 4

Which of the following can be a variable name in python?

Your score is

The average score is 73%

0%