Declare a Variable in Python

How to Declare a Variable in Python

Declare a Variable in Python

Python is a dynamically typed language, meaning you don’t need to specify the variable type or declare it before using it. This feature makes Python one of the most efficient and user-friendly programming languages. In Python, every variable is treated as an object, which simplifies coding and enhances flexibility.

Complete Python Course with Advance topics:-Click here

Before declaring variables in Python, you should follow these essential rules:

Rules for Declaring Variables:

  1. The variable name must begin with an underscore (_) or an alphabet.
  2. In variable names, special characters like @, #, %, ^, &, and * are not permitted.
  3. Variable names are case-sensitive. For example, age and AGE are treated as two different variables.
  4. Reserved keywords cannot be used as variable names. For instance, if, while, class, and def are reserved words in Python.

Now, let’s explore how to declare some basic variables.

Declaring Numbers in Python

Python supports three main types of numbers:

  • Integer
  • Floating-point numbers
  • Complex numbers

Python allows variables of any length, without restrictions. Here’s how to use and declare variables of the integer type:

num = 25  
print("The type of num:", type(num))  
print(num)  
  
float_num = 12.50  
print("The type of float_num:", type(float_num))  
print(float_num)  
  
complex_num = 2 + 5j  
print("The type of complex_num:", type(complex_num))  
print("Is complex_num a complex number?", isinstance(complex_num, complex))  

Output:

The type of num: <class 'int'>
25
The type of float_num: <class 'float'>
12.5
The type of complex_num: <class 'complex'>
Is complex_num a complex number? True

Declaring Strings in Python

Strings are sequences of Unicode characters in Python. Strings can be declared with single, double, or triple quotations. Here’s an illustration:

str_var = 'UpdateGadh'  
print(str_var)  
print(type(str_var))  
  
str_var1 = "Welcome to UpdateGadh"  
print(str_var1)  
print(type(str_var1))  
  
str_var2 = '''This is a string  
using triple  
quotes'''  
print(str_var2)  
print(type(str_var2))  

Output:

UpdateGadh
<class 'str'>
Welcome to UpdateGadh
<class 'str'>
This is a string
using triple
quotes
<class 'str'>

Multiple Assignments in Python

1. Assigning Multiple Values to Multiple Variables

Python allows assigning multiple variables on the same line. Here’s how:

x, y = 10, 20  
print(x, y)  

Output:

10 20

In the same order, the values are assigned and printed.

2.Giving Several Variables a Single Value

Additionally, you can give several variables the same value at once:

a = b = c = "UpdateGadh"  
print(a)  
print(b)  
print(c)  

Output:

UpdateGadh
UpdateGadh
UpdateGadh

Download New Real Time Projects :-Click here


python declare a variable in python without value
what is variable in python with example
python declare variable type
types of variables in python with example
rules for naming variables in python
data types in python
variables in python w3schools
what is a variable
for loop python
Declare a Variable in Python
declare an empty variable in python
declare a variable in python example
declare a variable in python w3schools

Post Comment