Python Tutorial 3---"Variables, Datatypes and Typecasting "

 Python Tutorial 3---

"Variables, Datatypes and Typecasting "

This PROGRAM or TUTORIAL about Python's  "Variables, Datatypes and Typecasting "

DOC

Variable:

A variable is a name given to any storage area or memory location in a program.

In simple words, we can say that a variable is a container that contains some information, and whenever we need that information, we use the name of that container to access it. Let's create a variable:

a = 34 # Variable storing an integer
b = 23.2 # Variable storing real number

Here a and b are variables, and we can use a to access 34 and to access 23.2. We can also overwrite the values in a and b

Data Types in Python:

Primarily there are following data types in Python.

  1. Integers (<class 'int'>): Used to store integers
  2. Floating point numbers (<class 'float'>): Used to store decimal or floating-point numbers
  3. Strings (<class 'str'>): Used to store strings
  4. Booleans (<class 'bool'>): Used to store True/False type values
  5. None: None is a literal to describe 'Nothing' in Python 

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 only start with an alphabet and underscore.
  • 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.

Examples of few valid variable names are harry_demode_mo, etc.

Python is a fantastic language that automatically identifies the type of data for us. It means we need to put some data in a variable, and Python automatically understands the kind of data a variable is holding.

type() Function in Python: type() function is a function that allows a user to find data type of any variable. It returns the data type of any data contained in the variable passed to it.

Note – We can't do arithmetic operations of numbers with strings i.e. we can't add a string to any number.

Note – We can add (concatenate) two or more strings and the strings will 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.

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.

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. For example, str("34") returns 34 which is of type integer (int)

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 allows 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 case.
If you enter 45 when input() is called, you will get "45" as a string


CODE

#Tut3 - Variables, Datatypes and Typecasting
#var1 = "Hello World"
#print(var1)
""""'var' denotes 'variables'
A variable is kind of a container in python."""
var1 = "Hello World" #It is a STRING Variable. STR stands for String.
var2 = 10 #It is a INTEGER Variable. INT stands for Integer.
var3 = 30.8 #It is a FLOAT Variable, it can understand decimal values.
#Example
"""print(type(var1)) #OUTPUT = <class 'str'>
print(type(var2)) #OUTPUT = <class 'int'>
print(type(var3)) #OUTPUT = <class 'float'>"""
#you can do mathematical calcuations between the datatypes, but you cant do that with 'strings'
#Example
#print (var2 + var3) #OUTPUT = 40.8
#print (var1 + var2) #there will be a error
# because you can not calaculate strings with oheer datatypes.
""" We can switch between functions by using these functions---
str()
int()
float()"""
#If we want to print a string multiple times we can just simply multiply by it required no. of times.
#Example, Multiplying Hello World 10 times---
#print(10 * "hello world\n")
#if you won't put a \n in the end your output will look dull, \n makes it cleaner by breaks the lines.
""" print (var2 + var3) in this case, it is a integer
we cant just simply multiply integers like the strings, there will be an error if we do that
so we have convert the integer's total value into string then it will be successful"""
#Example, Printing an Integer 10 times
#print(10 * str(int(var2 + var3)))
#Example of User Input---
print("Enter Your Number")
inpnum = input()
print ("You entered", int(inpnum)+10)
"""You have to convert the string into an integer either your program will not run"""

Comments

Popular posts from this blog

Python Tutorial 38 --- "Self & __init__() (Constructors)(OOPS 3)"

Python Exercise 7 --- "Snake, Water, Gun"

Python Tutorial 24 --- "Using Python External & Built In Modules"