You are on page 1of 19

Type Casting

• Type casting is converting the data type of a value to another data type.
• In programming, it is sometimes needed to change the data type of
information. This is known as type casting.

• To change data to an integer data type, use int().


• To change data to a string data type, use str().
• To change data to a float data type, use float().
• To change data to a Boolean data type, use bool().

A word of caution: trying to cast a string of text into an integer or float will cause
an error! Try int(“hello”) and see what happens!

Note: You cannot convert complex numbers into another number type.
Type Casting

x=1 #int
y=2.5 #float
z=“33” #str

print(x) print(str(x))
print(y) print (int(x))
print(z) print (float(x))
print(3*z)

z=int(z)
print(3*z)
Accepting user Input

• Use function input()


• Only accept “string” type

input(“What is your name”)


Type-> rose
Not accept data

name=input(“What is your name”)


print(“Hello!”, name)
Type-> rose
Accepting user Input

age=input(“How old are you”)


print(“Your age is “, age, “years old”)
Accepting user Input

age=input(“How old are you”)


print(“Your age is “, age, “years old”)

age=input(“How old are you”)


age= int(age)+1
print(“Your age is “, age, “years old”)
Volume of Rectangular Prism

width=int(input("width of rectangle"))
length=int(input("length of rectangle"))
high=int(input(“high of rectangle"))
volume= length * width*high
print("The volume of rectangle is : ", volume ,
“cubic meter")
Exercise

Area of Circle
Built-in Data Types

• In programming, data type


is an important concept.
• Variables can store data of
different types, and
different types can do
different things.
• Python has the following
data types built-in by
default.
Built-in Data Types
Built-in Data Types
Conditional Statements

• Conditional Statement in Python perform different computations or actions


depending on whether a specific Boolean constraint evaluates to true or
false.

• Conditional statements are handled by IF statements in Python.


IF…Else Statement

• Python if Statement is used for decision-making operations.

• It contains a body of code which runs only when the condition given in the
if statement is true.

• If the condition is false, then the optional else statement runs which
contains some code for the else condition.
IF…Else Flowchart
age=int(input("How old are you?"))
if age>=18:
print("You are an adult")
else:
print("You are a child")
If-elif-else

age=int(input("How old are you?"))


if age<0:
print("You haven't been born yet!")
elif age>=18:
print("You are an adult!")
else:
print("You are a child")
Logical operators (and, or and not)

Used to check if two or more conditional statements are true.

and operator: The Result is only True when both the First Value and the
Second Value are True.

or operator: The Result is True if the First Value is True, or if the Second
Value is True, or if both values are True.

not operator: The Boolean ‘not’ operator returns the logical opposite, so that
not True is False, and not False is True.
What about the Boolean ‘and’ operator? What Results would we get below?
First Value Second Value Result
False False
False True
True False
True True

What about the Boolean ‘or’ operator? What Results would we get below?
First Value Second Value Result
False False
False True
True False
True True

What about the Boolean ‘not’ operator? What Results would we get below?
Value Result
not True
not False
temp=int(input("What is the temperature outside?"))
if temp >= 0 and temp <= 30:
print("The temperature is good today!")
print("Go outside")
else temp<0 or temp>30:
print("The temperature is bad today")
print("Stay inside")
temp=int(input("What is the temperature outside?"))
if not(temp >= 0 and temp <= 30):
print("The temperature is bad today")
print("Stay inside")

else:
print("The temperature is good today!")
print("Go outside")

You might also like