Variables and data types

Variables and data types in Python programming language 

Get to know, what are data types and variables? With real life examples. How many data types are available with Python? And how you can use and implement them. 


Data Types in Python

What are Data types?

Data types are the classification of data.


Integers and floating point numbers (decimal numbers) are classified as numeric data type.


String and characters is classified as non-numeric data type.


True and False is classified as Boolean data type.


What are the data types in Python?

Basically there are 4 data types in Python programming language.

  • Integers
  • Floating
  • String
  • Boolean

Variables in Python

What are variables?

Variable is anything that is changing or can be changed.


For example time, which keeps on changing.

And our clothes which can be changed at any time.


What is a variable in Python programming language?

In Python or any other programming languages, a variable is a pair of variable name (a name assign to a memory location) and a value assigned to it.


Declaration and initialization of variables in Python

Declaration is just reserving a memory location, by specifying a variable name.


Unlike other programming languages in Python we do not have to specify the data type while declaring a variable.


Here is an example for declaration and initialization of a variable name 'a' with a string "Hello world".


Example:- a = 'Hello world'


If you put numbers in double or single quotes it will become string.


There are some rules for declaring a variable name.


Rules for declaring a variable name in Python

Following are the rules for declaring a variable name in Python or any other programming language (the rules are same for all high level programming languages).

  • Variable name should start with an alphabet.
    • Uppercase and lower case both are allowed.
  • Use of space is not allowed.
  • Should not start with a number or a special characters like (?$%^).
    • Use of numbers are allowed after some alphabets.
    • But can not use special characters.
  • Can use underscore  '_' .
  • Can not start with a number but, can start with an underscore.
  • Can not use Keywords as variable name.

Initialization of variables in Python

Initialization is nothing but, assigning a value to a variable name.

When we have declared a variable, at the same time we have to initialize it.

To initialize or assign a value, use equal to sign '='. 


Let's dive into coding...



How to check data types in python? 

To check which type of data is present with a variables, use type() function.


Example:-
Program

>>> num1 = 35
>>> type(num1)
<class 'int'>
>>> num2 = 3.0
>>> type(num2)
<class 'float'>
>>> a = "Hello world!"
>>> type(a)
<class 'str'>
>>> bo = True
>>> type(bo)
<class 'bool'>




Conclusion

You have learned the main 4 data types in Python, which are integer, float, string, Boolean. 


What are variables? Variable is anything that is changing or can be changed. Rules to create a variable and initialization of variables.


Use type() method to check which type of data it is.





Previous                    Next

If you have any query, feel free to ask.

Post a Comment

If you have any query, feel free to ask.

Post a Comment (0)