How to declare a variable in Python

In Python, you can declare a variable by assigning a value to it using the equals sign (=) operator. Here is the basic syntax for declaring a variable:

variable_name = value

Here, variable_name is the name of the variable, and value is the value that you want to assign to the variable.

For example, to declare a variable named x with the value of 10, you can write:

x = 10

You can also declare multiple variables in a single line by separating them with commas. For example:

a = 1
b = 2
c, d = 3, 4

In the above example, a is assigned the value 1, b is assigned the value 2, c is assigned the value 3, and d is assigned the value 4.


In Python, there are three types of numbers: integers, floating-point numbers, and complex numbers.

  1. Integers: Integers are whole numbers with no decimal point. In Python, integers are represented by the int data type. For example:
x = 10
y = -5
  1. Floating-point numbers: Floating-point numbers, also called “floats”, are numbers with a decimal point. In Python, floating-point numbers are represented by the float data type. For example:
a = 3.14
b = -0.5
  1. Complex numbers: Complex numbers are numbers with both a real part and an imaginary part. In Python, complex numbers are represented by the complex data type. You can create a complex number by specifying the real part and the imaginary part, separated by a + or - sign. For example:
z1 = 2 + 3j
z2 = 4 - 2j

In Python, you can perform mathematical operations on numbers using arithmetic operators such as +, -, *, /, %, and ** (for exponentiation). For example:

x = 10
y = 3
z = x + y    # z will be 13
w = x ** 2   # w will be 100 (10 squared)

It’s important to keep in mind that floating-point numbers are not exact, so you may encounter rounding errors when performing certain calculations.


In Python, a string is a sequence of characters, enclosed in either single quotes (‘…’) or double quotes (“…”). Here’s an example of how to create a string variable:

message = "Hello, world!"

In the above example, message is a variable containing a string value.

You can perform a wide range of operations on strings in Python. Here are a few examples:

  1. Concatenation: You can concatenate (join) two or more strings together using the + operator. For example:
greeting = "Hello"
name = "Alice"
message = greeting + " " + name

In the above example, message will be the string “Hello Alice”.

  1. String formatting: You can insert values into a string using placeholders (marked with curly braces) and the format method. For example:
name = "Bob"
age = 30
message = "My name is {} and I'm {} years old".format(name, age)

In the above example, message will be the string “My name is Bob and I’m 30 years old”.

  1. Substring: You can extract a substring (a portion of a string) using slicing. For example:
message = "Hello, world!"
substring = message[0:5]   # Gets the first five characters of the string

In the above example, substring will be the string “Hello”.

There are many other operations you can perform on strings in Python, including searching for substrings, replacing parts of a string, converting case, and more.

Multiple Assignments:

In Python, you can assign values to multiple variables in a single line using the multiple assignment syntax. The syntax for multiple assignment looks like this:

variable1, variable2, variable3 = value1, value2, value3

Here, variable1, variable2, and variable3 are the names of the variables you want to assign values to, and value1, value2, and value3 are the values you want to assign to those variables.

For example, you can assign values to three variables x, y, and z in a single line like this:

x, y, z = 10, 20, 30

In the above example, x will be assigned the value 10, y will be assigned the value 20, and z will be assigned the value 30.

You can also use multiple assignment to swap the values of two variables. For example:

x, y = y, x

In the above example, the values of x and y are swapped, so the value of x is now what y used to be, and vice versa.

Multiple assignment is a convenient way to assign values to multiple variables at once and can make your code more concise and readable.