Monday, May 24, 2021

python - Variables and Data Types


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




Variable – Container to store a value

Keywords – Reserved words in Python

Identifiers – class/function/variable name

Data Types:

Python is a fantastic language that automatically identifies the type of data for us. Data types identifies which type of data a variable can store.

Primarily there are the following data types in Python:


Floating point numbers





a = 71                                    #Identifies a as class<int>

b = 88.44                              #Identifies b as class<float>

name = “Sri”                  #Identifies name as class<Str>

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.
  • Variable names are case sensitive.
  • We cant use reserved words as variable name.

Examples of few valid variable names,

Harry, harry, one8, _akki, aakash, harry_bro, etc.

Operators in Python

The following are some common operators in Python:

Arithmetic Operators (+, -, *, /, etc.)

Assignment Operators (=, +=, -=, etc.)

Comparison Operators (==, >=, <=, >, <, !=, etc.)

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 “harry”, the user entered harry

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.

To add quick single line comment press ctrl + forward slash


Write a Python program to add two numbers.

print("the addition of a and b is:"a+b)

Write a Python program to find the remainder when a number is divided by Z(Integer).

print("the reminder is when a is divided by b :"a%b)

Check the type of the variable assigned using the input() function.

a=input("Please enter a number:")
print("the input type is:"type(a))

Please enter a number:23 the input type is: <class 'str'>

Use a comparison operator to find out whether a given variable a is greater than b or not. (Take a=34 and b=80)

print("checking if a is greater than b:"a>b)

checking if a is greater than b: False

Write a Python program to find the average of two numbers entered by the user.

a=input("Enter 1st number:")
b=input("Enter 2nd number:")
print("avearge of",a,"and ",b,"is :",avg)

Write a Python program to calculate the square of a number entered by the user.

a=int(input("Enter the number:"))
print("the square of the number",a,"is :",sqr)

No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...