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.


  1. x=55--> Correct
  2. y=037--> Incorrect
  3. z=0o98-->Incorrect as 'o' is not a digit so the number formed is incorrect
  4. 56thnumber=3300-->Incorrect as variable name can not begin with number
  5. length=450.17--> Correct
  6. !Taylor='Instant'-->Incorrect as variable name can not begin with special symbol "!"
  7. this variable=87.E02-->Incorrect as variable name can not have a blank space inbetween.
  8. float=.17E-03--> Correct as float is not a reserved word.value .00017 is assigned to variable float
  9. FLOAT=0.17E-03-->Correct as FLOAT is not a reserved word.value .00017 is assigned to variable FLOAT
Q2. Find out the error(s) the following code fragments:

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)

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. 

In this session we shall discuss about "Typecasting".

---------------------------------------------------------------------------------
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)
float() - 
  • constructs a float number from an integer literal, 
  • a float literal or 
  • a string literal (providing the string represents a float or an integer)
str() - 
  • 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:
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

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:
  1. >>>type(0)
  2. >>>type(int(0))
  3. >>>type(int('0'))
  4. >>>type('0')
  5. >>>type(1.0)
  6. >>>type(int(1.0))
  7. >>>type(float(0))
  8. >>>type(float(1.0))
  9. >>>type(3/2)
Match your result after executing the above statements.
You should be able to understand the result. If not,feel free to bring this up in the next class

Happy Learning.πŸ‘πŸ‘πŸ‘


Comments

  1. Good morning ma'am! Arjav Jain this side

    ReplyDelete
  2. Good Morning Ma'am
    Kavyansh Bagotra 9D

    ReplyDelete
  3. Good morning ma'am. Samarth raj 9D

    ReplyDelete
  4. Good morning ma'am
    My name is soumil arora

    ReplyDelete
  5. Good morning Ma'am
    My name is Ryan Rahuel Valentine from class 9D

    ReplyDelete
  6. Good Morning ma'am.I am Gracious Benny of class 9-D

    ReplyDelete
  7. Good Morning Ma'am
    My name is Joshua Walter from class 9-D.

    ReplyDelete
  8. Good Morning ma'am
    Aaron john

    ReplyDelete
  9. good morning mam aryan maheshwari here

    ReplyDelete
  10. Good morning mam I am Kritik Kuraria

    ReplyDelete
  11. Good morning mam I am Kritik Kuraria

    ReplyDelete
  12. Good Morning Mam I am Derick Joseph of 9D

    ReplyDelete
  13. Good morning Ma'am. I am Aryan Joseph Singh of 9D

    ReplyDelete
  14. Good morning ma'am I am Satwik Pruthi of Class 9D

    ReplyDelete
  15. Good morning madam I am RISHIT KUMAR

    ReplyDelete
  16. Good morning mam I am Gunamay prasad of class 9 D

    ReplyDelete
  17. Good morning mam I am Sandarbh of class 9-D

    ReplyDelete
  18. Good morning Ma'am I am Piyush Sharma of class 9-D

    ReplyDelete
  19. Good morning ma'am .
    My name is Ojas Khanna

    ReplyDelete
  20. Good Morning Mam I am Abhishek Dial 9 D

    ReplyDelete
  21. Good Morning Ma'am . This is Advait Mohanty of 9 D

    ReplyDelete
  22. Good afternoon mam I am ibrahim khan of 9D

    ReplyDelete

Post a Comment

Popular posts from this blog

Introduction

Python-Modes

Python-features