Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Different Types of Variables

User image

Published by

sanya sanya

Published at: 21st May, 2023
3.42 mins read

In programming, a variable is a container that holds a value. In Python, there are different types of variables that can be used to store different types of data. In this article, we will explore the different types of variables in Python, including numeric variables, string variables, boolean variables, list variables, tuple variables, dictionary and set variables.

1. Numeric Variables

Numerical Variables can itself store 3 types of variables namely,

  • Integers
  • Floating Point Number
  • Complex Number

Numeric variables are used to store numeric values such as integers, floating-point numbers, and complex numbers. Integers are whole numbers without any decimal point, while floating-point numbers have decimal points. Complex numbers are used to represent imaginary numbers.

To create a numeric variable, you simply assign a value to a variable name.

For example:

Age = 21 # int (integer data type) Height = 168.58 # float (floating point number data type) location_of_point = 2 + 3j # complex number location of point in cartesian plane)

In this code, Age is an integer variable, Height is a floating-point variable, and location_of_point is a complex variable.

Numeric variables support all the standard mathematical operations, such as addition, subtraction, multiplication, and division.

For example:

#lets take two sides of rectangle and calculates its perimeter and area Side_1 = 50 Side_2 = 20 Area = Side_1 * Side_2 #Multiplication Perimeter = 2 * ( Side_1 + Side_2) #Addition

In this code, the lengths of the sides of the rectangle are given as 50 and 20. Area of rectangle is calculated as product of both the sides and perimeter to be twice the sum of both the sides.

Numeric variables can also be used in comparison operations, such as ==, >, <, >=, and <=. For example:

2. String Variables

String variables are used to store text values. Strings are created by enclosing the text in quotes, either single quotes or double quotes.

For example:

name = "John" #single quoted string message = 'Hello World' # double quoted string

In this code, name is a string variable that stores the name "John", and message is a string variable that stores the message "Hello, world!". You can also use triple quotes to create multi-line strings, like this: In this code, paragraph is a string variable that stores a multi-line string.

For example

multiline = '''This is a multi-line string. It can span multiple lines. ''' #Multiline string using three quotations

String variables support several operations, such as concatenation and slicing. Concatenation is used to join two or more strings together, while slicing is used to extract a part of a string.

For example:

a = 'Hello' b = 'world' c = a + ' ' + b d = a[0:5]

In this code, c is equal to "Hello world", and d is equal to "Hello". This concept will be discussed in detail further. String variables can also be used in comparison operations, just like numeric variables.

3. Boolean Variables

Boolean variables are used to store True or False values. They are often used in conditional statements and loops to control the flow of the program.

For example:

is_raining = True has_umbrella = False

In this code, is_raining is a boolean variable that stores the value True, and has_umbrella is a boolean variable that stores the value False.

Some key points to be noted,

  1. A variable name can hold a single type of value. For example, if variable a has been defined int type, then it can store only integer.
  2. You can use a variable name only once inside your program. For example, if a variable a has been defined to store an integer value, then you cannot define a again to store any other type of value.
  3. Variables in Python are dynamically typed, which means you don't have to specify the type of a variable when you declare it.
  4. Variables in Python are case sensitive, which means name and Name are two different variables.

Library

WEB DEVELOPMENT

FAANG QUESTIONS

On this page

1.

2.

3.