Type Conversion
Type Conversion అంటే ఏమిటి?
ఒక data type valueను మరొక data typeగా మార్చడాన్ని Type Conversion అంటారు. ఉదాహరణకు, stringగా ఉన్న "25"ను integerగా మార్చవచ్చు.
Type Conversion ఎందుకు ఉపయోగిస్తాము?
కొన్ని సందర్భాల్లో మనకు వచ్చిన value సరైన data typeలో ఉండదు. అప్పుడు దానితో calculations చేయడానికి లేదా textగా ఉపయోగించడానికి type conversion అవసరం అవుతుంది.
int()ఉపయోగించి valueను integerగా మార్చవచ్చు.float()ఉపయోగించి valueను decimal numberగా మార్చవచ్చు.str()ఉపయోగించి valueను stringగా మార్చవచ్చు.
ఇది ఎలా పనిచేస్తుంది?
Conversion functionలో మార్చవలసిన valueను ఉంచాలి. ఉదాహరణకు int("25") string valueను integer valueగా మారుస్తుంది.
సరైన formatలో ఉన్న valueలను మాత్రమే convert చేయగలం. ఉదాహరణకు int("hello") పనిచేయదు, ఎందుకంటే helloను integerగా మార్చలేము.
Simple Explanation
Type Conversion అంటే ఒక type valueను మరో type valueగా మార్చడం.
int() integer కోసం, float() decimal number కోసం, str() text కోసం ఉపయోగిస్తాము.
ఉదాహరణకు "100" ఒక string. దాన్ని int("100")గా మార్చిన తర్వాత దానితో addition, multiplication వంటి calculations చేయవచ్చు.
float(10)ను ఉపయోగిస్తే 10.0 వస్తుంది. str(25)ను ఉపయోగిస్తే "25" వస్తుంది.
Real-Life Example
ఒక shopలో product priceను మొదట textగా నమోదు చేశారని అనుకుందాం. ఆ priceతో calculation చేయాలంటే దాన్ని numberగా మార్చాలి. ఉదాహరణకు "50"ను int("50")గా మార్చి quantityతో గుణించవచ్చు. Calculation పూర్తయ్యాక resultను textగా చూపించడానికి str() ఉపయోగించవచ్చు.
Code Examples
Stringను Integerగా మార్చడం
number_text = "25"
number = int(number_text)
print(number + 5)
మొదట number_textలో string valueని ఉంచాము. int() ఉపయోగించి దాన్ని integerగా మార్చాము. తరువాత 5ను కలిపాము.
30
Integerను Floatగా మార్చడం
whole_number = 10
decimal_number = float(whole_number)
print(decimal_number)
float() function integer valueను decimal numberగా మారుస్తుంది. అందువల్ల 10, 10.0గా మారుతుంది.
10.0
Numberను Stringగా మార్చడం
age = 20
age_text = str(age)
print("Age: " + age_text)
str() function integer valueను stringగా మారుస్తుంది. Stringగా మార్చిన తర్వాత దాన్ని మరో stringతో కలపవచ్చు.
Age: 20
Floatను Integerగా మార్చడం
price = 19.99
whole_price = int(price)
print(whole_price)
Floatను integerగా మార్చినప్పుడు decimal part తొలగిపోతుంది. ఇది rounding కాదు. కాబట్టి 19.99, 19గా మారుతుంది.
19
Line-by-Line Explanation
ఈ lineలో 25 అనే text valueను number_text variableలో ఉంచుతాము. Quotes ఉన్నందున దీని type string.
ఈ line number_textలోని stringను int() function ద్వారా integerగా మారుస్తుంది. Python ఈ converted valueను number variableలో ఉంచుతుంది.
ఇక్కడ integer numberకు 5ను కలిపి resultను screenపై చూపిస్తాము. number value 25 కావడంతో output 30 అవుతుంది.
ఈ lineలో 10 అనే integer valueను whole_number variableలో ఉంచుతాము.
float() function whole_numberలోని integer valueను float valueగా మారుస్తుంది. అందువల్ల decimal_number విలువ 10.0 అవుతుంది.
ఈ lineలో converted float valueను screenపై చూపిస్తాము.
ఈ lineలో 20 అనే integer valueను age variableలో ఉంచుతాము.
str() function ageలోని integer valueను stringగా మారుస్తుంది. Converted value age_textలో నిల్వ అవుతుంది.
ఇక్కడ రెండు stringsను + operatorతో కలిపి print చేస్తాము. అందువల్ల Age: 20 అనే text కనిపిస్తుంది.
ఈ lineలో 19.99 అనే decimal valueను price variableలో ఉంచుతాము. దీని type float.
int() function float valueను integerగా మారుస్తుంది. Decimal part తొలగిపోవడంతో whole_price విలువ 19 అవుతుంది.
ఈ line converted integer valueను screenపై చూపిస్తుంది.
Try It
Try this Python code:
number_text = "42"
number = int(number_text)
print(number + 8)
Practice
Practice: price_text = “250” మరియు quantity_text = “3” అనే రెండు string values ఉన్నాయి. వాటిని integersగా మార్చి total priceను calculate చేయండి. చివరగా totalను print చేయండి.
Hint: రెండు valuesపై int() ఉపయోగించండి. తరువాత priceను quantityతో multiply చేయండి.
Practice: temperature_text = “36.5” అనే string valueను floatగా మార్చి print చేయండి.
Hint: float(temperature_text) ఉపయోగించండి.
Practice: score = 95 అనే integer valueను stringగా మార్చి Score: 95 అనే outputను print చేయండి.
Hint: str(score)ను మరో stringతో + operator ద్వారా కలపండి.
Common Mistakes
ముందుగా int(“25”) ఉపయోగించి stringను integerగా మార్చండి. తరువాత calculation చేయండి.
Decimal value కోసం ముందుగా float(“12.5”) ఉపయోగించండి.
int()లో digitsతో ఉన్న సరైన numeric string మాత్రమే ఇవ్వాలి.
int(19.99) result 19 అవుతుంది. int() decimal partను తొలగిస్తుంది; rounding చేయదు.
20ను str(20)గా మార్చి తరువాత stringతో కలపండి.
Interview Questions
Type Conversion అంటే ఏమిటి?
Answer: ఒక data type valueను మరొక data typeగా మార్చడాన్ని Type Conversion అంటారు.
Stringను integerగా మార్చడానికి ఏ function ఉపయోగిస్తారు?
Answer: int() function ఉపయోగిస్తారు. ఉదాహరణ: int(“25”)
Integerను floatగా మార్చడానికి ఏ function ఉపయోగిస్తారు?
Answer: float() function ఉపయోగిస్తారు. ఉదాహరణ: float(10) result 10.0.
Valueను stringగా మార్చడానికి ఏ function ఉపయోగిస్తారు?
Answer: str() function ఉపయోగిస్తారు. ఉదాహరణ: str(25) result “25”.
int(19.99) output ఏమిటి?
Answer: Output 19. int() decimal partను తొలగిస్తుంది.
Exam Questions
క్రింది code output ఏమిటి? x = int(“15”) print(x + 5)
Answer:
20
క్రింది code output ఏమిటి? print(float(7))
Answer:
7.0
int(), float(), str() functions ఉపయోగాలను రాయండి.
Answer:
int() valueను integerగా, float() valueను floatగా, str() valueను stringగా మారుస్తాయి.
క్రింది codeలో error ఎందుకు వస్తుంది? print(“10” + 5)
Answer:
String మరియు integerను నేరుగా కలపలేము. 5ను str(5)గా మార్చి “10” + str(5) అని రాయాలి.
int(“12.5”) ఎందుకు పనిచేయదు?
Answer:
12.5 ఒక decimal string. దాన్ని integerగా మార్చడానికి ముందు float(“12.5”) ఉపయోగించాలి.
Quick Revision
- Type Conversion అంటే ఒక data typeను మరొక data typeగా మార్చడం.
- int() string లేదా numberను integerగా మార్చుతుంది.
- float() valueను decimal numberగా మార్చుతుంది.
- str() valueను stringగా మార్చుతుంది.
- int()తో floatను మార్చినప్పుడు decimal part తొలగిపోతుంది.
- Numeric formatలో లేని stringను int() లేదా float()తో మార్చలేము.
- Calculationకు ముందు numeric stringలను int() లేదా float()తో మార్చాలి.
