Skip to main content

2. Variables and Data types in Python

 

Variable

A variable is the name given to a memory location in a program 
For example-
a = 30                                                           - Variable = Container to store a value
b = "Aariz"                                                   - Keywords = Reserved words in Python
c = 67.22                                                       - Identifiers = class/function/variable name



Data Types 

Primarily there are are following date 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 = 76                                                             ⇒ Identifies a as class<int>
b = 58.24                                                        ⇒  Identifies b as class<float>
c = "Aariz"                                                     ⇒  Identifies c as class<str>

Rules for defining a Variable name        - Also applies to other Identifiers

➡️ A variable name can contain alphabets, digits and underscores.
➡️ A variable name can only start with an alphabet and underscore.
➡️ A variable name can't start with a digit.
➡️ No while space is allowed to be used inside a variable name.
Example of a few variable name are -
aariz, one8, seven_, _seven etc.

Operators in Python

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 integer conversion
float(34) ⇒ 34.0                                      ⇒ Integer to float conversion
                               ...and so on
Here "31" is 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 "aariz" the user entered aariz
It is important to note that the output of input is always a string (even if a number is entered) ⇒ If a is "34" user entered 34 



Practice Set

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

Comments

Popular posts from this blog

1. Modules, Comments & pip in Python

Let's write our very first python program. Create a file called hello.py and paste the below code in it: print("Hello World")                                                                  ➡️  print is a function Execute this file(.py file) by typing python hello.py on your terminal and you will see Hello World printed on the screen. Modules A module is a file containing code written by somebody else(usually) which can be imported and used in our programs.  Pip Pip is the package manager for python you can use pip to install a module on your system  ➡️  pip install numpy installs numpy module Types of modules There are two types of modules in Python 1. Built in modules             ➡️ Pre installed in python 2. External modules          ...

3. Strings

 Strings String is a data type in python. String is a sequence of characters enclosed in quotes. We can primarily, write a string in these three ways : Single quoted strings ➡️ a = 'Aariz' Double quoted strings ➡️ b = "Aariz" Triple quoted strings ➡️ c = "'Aariz"' String Slicing A string in python can be sliced for getting a part of the string. Consider the following string: name = "Aariz"                                    ➡️ length = 5               ↑↑↑↑↑               01234                The index in a string starts from 0 to (length-1) in Python. In order to slice a string, we use the following syntax: sl = name[int_start:int_end]                       ↙️            ↘️ first index includ...