Type Casting
02 / شرح الدرس مكتوبًاmain.py
نص شرح المعلم والكود الذي كتبه ونتيجة تشغيله، بنفس ترتيب الدرس.
تمرين
what the outputs
# ==========================================
# Python for Beginners
# Lecture 7: Type Casting in Python
# ==========================================
# Lecture Contents:
# 1. What Is Type Casting?
# 2. Converting to Integer Using int()
# 3. Converting to Float Using float()
# 4. Converting to String Using str()
# 5. Practical Examples
# 6. Common Type Conversion Errors
# 7. Practice Task: Simple Calculator
# ==========================================
# age = "21"
# # print(age) # 21 =>string
# # print(type(age)) #str
# #Convert to integer
# age = int(age)
# print(age) # 21 => integer
# print(type(age)) #int
# price = 150.75 # float
# price = int(price) #int
# print(price) # 150 => int
#float
price = "150.75" # float
price = float(price) #string => float
# print(price) # 150.75 => float
# print(type(price)) # 150.75 => float
number = 14 # int
number = float(number) #float , 14 => 14.00
# print(number)
#string => str
# age = 20 # integer
# age = str(age) # 20 => integer , "20" => string
# print(age) # 20
# print(type(age)) # str
#Type Casting Examples
age = "20" #str
# age = int(age) # int
age = int("20") # int("20")
price = "99.5" #str
price = float(price) # float
score = 95 #int
score = str(score) #str
# print(age,type(age)) #20 => int
# print(price,type(price)) #99.5 => float
# print(score,type(score)) #95 => "95" => str