variables and data types in python

Variables are identifiers of a physical memory location, which is used to hold values temporarily during program execution.

Variable in Python?

A variable is a name given to a memory location in a program. For example

a=30

b=”Programiz.net”

c=71.22

In Python, like many other programming languages, there is no need to define variables in advance. As soon as a value is assigned to a variable, it is automatically declared. This is why Python is called a dynamically typed language.

The syntax for creating variables in Python is given below:Syntax:

<variable-name> = <value>

Assigning Values to Variables

Python interpreter can determine what type of data is stored, so before assigning a value, variables do not need to be declared.

Usually, in all programming languages, equal sign = is used to assign values to a variable. It assigns the values of the right side operand to the left side operand.

The left side operand of = operator is the name of a variable, and the right side operand is value.Example:

name = "Packing box" # A string
height = 10 # An integer assignment
width = 20.5 # A floating point

print (name)
print (height)
print (width)

Output:

Packing box
10 
20.5

In the above code snippet, the variable name ‘height’ is storing a value 10, and since the value is of type integer, the variable is automatically assigned the type integer.
Another variable name ‘width’ is assigned with floating type value. Then both the values are printed or displayed using the ‘print’ statement.

Rules for defining a variable name: (Also applicable to other identifiers)

  • A variable name can contain alphabets, digits, and underscore.
  • A variable name can only start with an alphabet and underscore.
  • A variable can’t start with a digit.
  • No white space is allowed to be used inside a variable name.

Examples of few valid variable names,

Program, one8, _akki, aakash, Programiz_net, etc.

Python Variable Deletion

Python also provides the facility to delete a variable from memory. For this, the del command is used. The following is the general syntax for deleting a variable in Python:Syntax:

del <variable-name>

Variable – Container to store a value

Keywords – Reserved words in Python

Identifiers – class/function/variable name

Data Types:

Primarily there are the following data types in Python:

  1. Integers
  2. Floating point numbers
  3. Strings
  4. Booleans
  5. None

Python is a fantastic language that automatically identifies the type of data for us.

a = 71                                    #Identifies a as class<int>

b = 88.44                              #Identifies b as class<float>

name = “Programiz.net”                  #Identifies name as class<Str>

Python data types are different in some aspects from other programming languages. It is simple to understand and easy to use. Because Python is interpreted programming language and Python interpreter can determine which type of data is storing, so no need to define the data type of memory location.

The data type determines:

  • The possible values for that type.
  • The operations that can be done with that values.
  • Conveys the meaning of data.
  • The way values of that type can be stored.

Data Types available in Python

Everything in Python programming is an object, and each object has its own unique identity(a type and a value).

There are many native(built-in) data types available in Python.

Some important are:

  • Numbers: An are integers (such as 1, 2, 3…), floats (such as 2.6, 4.8, etc.), fractions (such as ½. ¾, etc.), or even complex numbers.
    • int (signed integer)
    • float
    • long
    • complex
  • Sequences:
    • Strings: Sequence of Unicode characters, like an HTML document.
    • Bytes/Byte array: Any type of file.
    • Lists: An ordered sequence of values.
    • Tuples: An ordered, immutable sequence of values.
  • Boolean: Holds either true or false values.
  • Sets: An unordered container of values.
  • Dictionaries: A key-paired values set in an unordered way.

Because Python is a pure object-oriented programming language,  so other data types are also available.

  • Module
  • Function
  • Class
  • Method
  • File
Operators in Python

The following are some common operators in Python:

  1. Arithmetic Operators (+, -, *, /, etc.)
  2. Assignment Operators (=, +=, -=, etc.)
  3. Comparison Operators (==, >=, <=, >, <, !=, etc.)
  4. Logical Operators (and, or, not)

type() function and Typecasting

type function is used to find the data type of a given variable in Python.

a = 31

type(a)                      #class<int>

b = “31”

type(b)                      #class<str>

A number can be converted into a string and vice versa (if possible)

There are many functions to convert one data type into another.

Str(31)           # ”31” Integer to string conversion

int(“32”)       # 32 String to int conversion

float(32)       #32.0 Integer to float conversion

… and so on

Here “31” is a string literal, and 31 is a numeric literal.

input() function

This function allows the user to take input from the keyboard as a string.

a = input(“Enter name”)               #if a is “Programiz.net”, the user entered Programiz.net

Note: The output of the input function is always a string even if the number is entered by the user.

Suppose if a user enters 34, then this 34 will automatically convert to “34” string literal.

Exercise – Variables and Data Types On Python

  1. Write a Python program to add two numbers.
  2. Write a Python program to find the remainder when a number is divided by Z(Integer).
  3. Check the type of the variable assigned using the input() function.
  4. Use a comparison operator to find out whether a given variable a is greater than b or not. (Take a=34 and b=80)
  5. Write a Python program to find the average of two numbers entered by the user.
  6. Write a Python program to calculate the square of a number entered by the user.

Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *