Variables, Datatypes and Typecasting

Variable -:

A variable is a name that is given to any storage area or memory location in a program. Actually, Variable doesn’t hold value but it is the name given to any memory address in RAM. It means the variable is the way to access that memory address of Ram so that we can store and manipulate data in that memory address or memory block. It’s the name of a memory location, this sounds like some advanced stuff but worries not. Variable is a name given to a memory location that is all you need to know at this point in this course!

In simple words, we can say that variable is a container that contains some kind of information and whenever we need that information then we simply use that container to access its content.

Rules for defining a variable in Python :

  • A variable name can contain alphabets, digits, and underscores (_). For E.g. : demo_xyz = ‘It’s a string variable’
  • A variable name can start with an alphabet and underscore only.

For E.g. : _demo, de_mo, etc. are all valid variable names

  • It can’t start with a digit. 5harry is illegal and not allowed
  • No white-space is allowed to be used inside a variable name.
  • Also, reserved keywords are not recommended to be used as variable names

Python is an amazing language that automatically identifies the type of data for us. It means we just need to put some data in a variable and Python automatically understands what kind of data variable is holding. Cool, isn't it?

Have a look at the code below:

 

type() Function: type() function is a function which allows user to find data type of any variable. It returns the data type of any data.
Have a look at the code below which depicts the use of type function:

 

Note – We can’t do arithmetic operations within strings i.e. we can’t add a string to any number.

 

Note – We can add (concatenate) two or more strings as string and string can be concatenated to return another string.

 

Typecasting :

Typecasting is a way to change the data type of one data to another i.e. it changes the data type of any variable to some other data type.

I know it’s a bit confusing but let me tell you in a simple manner. Suppose there is a string “34” (note string not integer since it is enclosed in double-quotes) and as we know we can’t add this to an integer number let’s say 6. But to do so we can typecast this string to int data type and then we can add 34+6 to get the output as 40. Have a look at the program below:

 

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

str() – this function allows us to convert some other data type into a string.

int() – this function allows us to convert some other data type into an integer.

float() – this function allows us to convert some other data type into floating-point number i.e. a number with decimals.

input() Function – This function allow the user to receive input from the keyboard into the program as a string. 
input() function always takes input as a string i.e. if we ask the user to input a number even then it will take it as a string and we will have to typecast it into another data type as per the use.
If you enter 45 you will get "45" as a string

 

FOR MORE DETAIL VISIT YOUTUBE LINK -https://youtu.be/z1-zfCvxybI


Comments

Popular posts from this blog

What is Python?

String Slicing And Other Functions

Lists And Tuples