Search Bar

Sunday 30 April 2023

Python Programming Tut#05|Variables in Python

 Python Tutorial 05

Variables in Python

In Python, a variable is a container that stores a value, which can be a number, a string, a boolean, lists, tuples, dictionaries or any other data type. You can assign a value to a variable using the equal sign (=).

For example:

x = "Hello, World!"

In this example, x is the name of the variable, and "Hello, World!" is the value that it stores.

Python Programming Tutorial 05|Variables in Python
Python Programming Tutorial 05|Variables in Python
Example 2
x = 7
In this example, x is the variable, and 7 is the value that it stores.
Python Programming Tutorial 05|Variables in Python
Python Programming Tutorial 05|Variables in Python

You can use the variable in expressions, just like you would use the value itself:

y = x + 2

In this example, y is assigned the value of x + 2, which is 9.
Python Programming Tutorial 05|Variables in Python
Python Programming Tutorial 05|Variables in Python

You can also assign a value to multiple variables at once, like this:

x, y, z = 7, 8, 9

In this example, x, y, and z are all variables that are assigned values of 7, 8, and 9, respectively.
Python Programming Tutorial 05|Variables in Python
Python Programming Tutorial 05|Variables in Python

Python variables Some Rules

Python variables have some rules to follow:
  • A variable name must start with a letter or an underscore (_).
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores.
  • Variable names are case sensitive. For example, name and Name are two different variables.
  • You cannot use reserved words as variable names. For example, if, else, while, and for are all reserved words.

0 comments:

Post a Comment