Skip to main content

Variables in Python and checking the type of variable

What are Variables?

Variables are nothing but the reserved memory locations where you can store some values. When a variable is created it automatically allocates some memory for itself.


How declaration in python is different from other programming languages??

If you have been to other programming languages previously like java ,c or c++ then you need to give the datatype of the variable you want to use. But Python is a bit more flexible and intelligent you need not to specify the datatype of variable, python will automatically detect the datatype of variable will perform the operations accordingly.Python variables do not need explicit declaration to reserve memory space.

Assigning values to variables 
You can simply assign values to variables by just putting an equals to(=) sign between variable and value.You can simply create a python file in pycharm 
eg.
 a=10  # An integer variable

 b=45.67 # floating point variable

 c="Hii this is a string "

 print(a)
 print(b)
 print(c)
10
45.67
Hii this is a string

Multiple Assignments
You can assign multiple values to different variables
eg.
 a=b=c=10                     # integer variables print(a)
 print(b)
 print(c)


Output:

10
10
10

eg.
 a,b,c=10,20,30                     # An integer variable print(a)
 print(b)
 print(c)

Output:

10
20
30

How to check the type of variables?
To check the type of variable use we can simply use a built-in method called type().

SYNTAX:
type(object)
type(name, bases, dict)
We will understand it more clearly using examples:
eg.
a=10  # An integer variable
b=45.67 # floating point variable

c="Hii this is a string "
d=[1,2,4]


e={}

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
Output:


<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>

Here we can see that type() method is returning the type of variables used in program






Comments

Popular posts from this blog

How to install python IDE ? IDLE and Pycharm

How to install Python IDE on Windows 10(Pycharm and IDLE)? Pycharm is the best Python IDE available.It comes with many in -built features. We will discuss its shortcuts in our next blogs.Pycharm is a cross-platform editor developed by jetbrains.   Installing Python Step 1) Visit the official website of python or click the below link              https://www.python.org/downloads/  .  Step 2) Click on Download Python               Step 3) After the download is complete ,open the exe file and click               on Install now  Step 4) After the successful installation it will show you a message         Setup was successful Step 5) Now go to your windows search bar and type "IDLE"              Press Enter and you will see that idle is opened Installing Pycharm St...

String Slicing in python

String Slicing Python  Slicing refers to extracting some words from a string. As in many other languages there are many different concepts for extracting a letters or words from a string ,in python we are having the concept of slicing. We can also the same things using looping, we will learn about it in our next tutorials. Indexing in Strings Before starting slicing, we need to understand the indexing in python. We have the concept of positive indexing  and  negative indexing.  Positive indexing starts from 0 to (length of string).It goes from forward to backward. Negative indexing starts from -1 to -(length of string + 1). It is illustrated in below example Below are some examples of how to use string slicing to extract characters from string. Accessing the characters in a string using positive index eg. string= "We are studying string slicing" s=string[ 0 ] # A single character will be printed present at index 0 w=string[ 4 ] # A...