Python-typecasting
Good Morning Boys
Before I begin today's session, I would like to inform that
if you do not use your name as the user name and use weird names then I shall not allow you in the Google Meet Class whenever next I conduct.
For toady's session, Google meet ID:
Let us first discuss the model answers for yesterday's questions:
Q1. From the following, find out which assignment statement will produce an error.
State reason(s) too.
- x=55--> Correct
- y=037--> Incorrect
- z=0o98-->Incorrect as 'o' is not a digit so the number formed is incorrect
- 56thnumber=3300-->Incorrect as variable name can not begin with number
- length=450.17--> Correct
- !Taylor='Instant'-->Incorrect as variable name can not begin with special symbol "!"
- this variable=87.E02-->Incorrect as variable name can not have a blank space inbetween.
- float=.17E-03--> Correct as float is not a reserved word.value .00017 is assigned to variable float
- FLOAT=0.17E-03-->Correct as FLOAT is not a reserved word.value .00017 is assigned to variable FLOAT
1. temperature=90
print(temprature)-----> Error--spellings not the same as variable created,so will error.
2. a=30
b=20
print(a And b)-----> error--Incorrect syntax of print() as if we want to print the
values of variables a and b in a single print statement then, correct print
command is
print(a,b)
values of variables a and b in a single print statement then, correct print
command is
print(a,b)
3. X=24
4=X --->Error-- In an assignment statement, variable name has to be to
the left of the assignment while value has to be to its right.
the left of the assignment while value has to be to its right.
---------------------------------------------------------------------------------
By the end of this session, you will be able to
- Create new variables by changing the type of an already existing variable.
- Assign values to a variable using typecasting and also predict the values that a variable may get in due course of program execution
- Understand the working of typecasting and its impact on the multiple variables.
TYPECASTING
Specifying a Variable Type
There may be times when we have assigned a value to a variable but in due course of the program, we want to change the data type of that variable. This can be done with Type Casting, often referred to as "Casting" also.
Casting in python is therefore done using special functions:
int() - - constructs an integer number from an integer literal,
- a float literal (by rounding down to the previous whole number),
- or a string literal (providing the string represents a whole number)
- constructs a float number from an integer literal,
- a float literal or
- a string literal (providing the string represents a float or an integer)
- constructs a string from a wide variety of data types, including strings, integer literals and float literals
Example
Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
So, in the following program,
x = int(1)
y = int(2.8)
z = int("3")
print(x)
print(y)
print(z)
The Output will be:
1
2
3
Example
Floats:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
So, for the following program
x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")
print(x)
print(y)
print(z)
print(w)
The output will be:
1.0
2.8
3.0
4.2
Strings:
Strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
For the following program:
x = str("s1")
y = str(2)
z = str(3.0)
print(x)
print(y)
print(z)
The output will be:
s1
2
3.0
3.0
Please note that in the above statements
y=str(2) and z=str(3.0)
y and z will be string type variables as they are holding "2" or '2' and "3.0" or '3.0' respectively.
"2" or '2' and "3.0" or '3.0' " are strings and not numbers.
So, if the print statement is
print(y+z)
The output will be
23.0 (as concatenation will take place here)
-------------------------------------------------------
EXERCISE
What will be returned by Python as result of the following statements:
- >>>type(0)
- >>>type(int(0))
- >>>type(int('0'))
- >>>type('0')
- >>>type(1.0)
- >>>type(int(1.0))
- >>>type(float(0))
- >>>type(float(1.0))
- >>>type(3/2)
You should be able to understand the result. If not,feel free to bring this up in the next class
Happy Learning.πππ
Good morning ma'am! Arjav Jain this side
ReplyDeleteGood Morning Ma'am
ReplyDeleteKavyansh Bagotra 9D
Good morning ma'am. Samarth raj 9D
ReplyDeleteGood morning ma'am
ReplyDeleteMy name is soumil arora
Good morning Ma'am
ReplyDeleteMy name is Ryan Rahuel Valentine from class 9D
Good Morning ma'am.I am Gracious Benny of class 9-D
ReplyDeleteGood Morning Ma'am
ReplyDeleteMy name is Joshua Walter from class 9-D.
Good Morning ma'am
ReplyDeleteAaron john
good morning mam aryan maheshwari here
ReplyDeleteGood morning mam I am Kritik Kuraria
ReplyDeleteGood morning mam I am Kritik Kuraria
ReplyDeleteGood Morning Mam I am Derick Joseph of 9D
ReplyDeleteGood morning Ma'am. I am Aryan Joseph Singh of 9D
ReplyDeleteGoswami samaksh
ReplyDeleteGood morning ma'am I am Satwik Pruthi of Class 9D
ReplyDeleteGood morning madam I am RISHIT KUMAR
ReplyDeleteGood morning mam I am Gunamay prasad of class 9 D
ReplyDeleteGood morning mam I am Sandarbh of class 9-D
ReplyDeleteGood morning Ma'am I am Piyush Sharma of class 9-D
ReplyDeleteGood morning ma'am .
ReplyDeleteMy name is Ojas Khanna
Good Morning Mam I am Abhishek Dial 9 D
ReplyDeleteGood Morning Ma'am . This is Advait Mohanty of 9 D
ReplyDeleteGood afternoon mam I am ibrahim khan of 9D
ReplyDelete