type()
Python లో type()
type() అనేది ఒక value లేదా variable ఏ data type కు చెందినదో తెలుసుకోవడానికి ఉపయోగించే built-in function.
type() ఎందుకు ఉపయోగిస్తాము?
ఒక variable లో number ఉందా, text ఉందా, decimal value ఉందా లేదా True/False value ఉందా తెలుసుకోవడానికి type() ఉపయోగిస్తాము.
Syntax
type(value)
ఇక్కడ value స్థానంలో variable పేరు లేదా నేరుగా ఒక value ఇవ్వవచ్చు. Python ఆ value యొక్క type ను చూపిస్తుంది.
సాధారణ Data Types
int– పూర్తి సంఖ్యలు, ఉదాహరణకు 10float– decimal సంఖ్యలు, ఉదాహరణకు 10.5str– text లేదా string, ఉదాహరణకు Hellobool– True లేదా False
type() ఎలా పనిచేస్తుంది?
Python ముందుగా variable లో ఉన్న value ను చూస్తుంది. ఆ value ఏ data type కు చెందినదో గుర్తించి, దాని type information ను return చేస్తుంది. print() తో కలిపి ఉపయోగిస్తే ఆ type screen పై కనిపిస్తుంది.
ఎక్కడ ఉపయోగపడుతుంది?
- Program లో ఉన్న data ను అర్థం చేసుకోవడానికి
- Debugging సమయంలో తప్పు data type ఉందో తెలుసుకోవడానికి
- User input లేదా ఇతర data ను పరిశీలించడానికి
- ఒక variable లో ఉన్న value మారినప్పుడు దాని type తెలుసుకోవడానికి
Simple Explanation
type() ను ఒక value యొక్క identity card లాగా ఊహించవచ్చు. Value ఏ రకానికి చెందినదో ఇది చెబుతుంది.
ఉదాహరణకు 10 ఒక integer, 3.14 ఒక float, "Python" ఒక string, True ఒక boolean. వీటి type తెలుసుకోవడానికి type() ఉపయోగిస్తాము.
print(type(10))
దీని output <class 'int'> గా వస్తుంది. ఇక్కడ int అంటే integer అని అర్థం.
Real-Life Example
ఒక library లో పుస్తకాలను categoryలుగా విభజిస్తారు: Science, History, Story వంటివి. అలాగే Python కూడా ప్రతి value ను దాని data type ఆధారంగా గుర్తిస్తుంది. ఒక పుస్తకం ఏ categoryలో ఉందో తెలుసుకున్నట్లే, ఒక value ఏ typeలో ఉందో తెలుసుకోవడానికి type() ఉపయోగిస్తాము.
Code Examples
వివిధ Values యొక్క Types తెలుసుకోవడం
print(type(25))
print(type(4.5))
print(type("Python"))
print(type(True))
ఈ program నాలుగు వేర్వేరు values యొక్క data types ను చూపిస్తుంది. పూర్తి సంఖ్యకు int, decimal సంఖ్యకు float, text కు str, True లేదా False కు bool వస్తాయి.
Variables యొక్క Types తెలుసుకోవడం
name = "Ravi"
age = 20
height = 5.8
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
ముందుగా నాలుగు variables లో వేర్వేరు values నిల్వ చేశాము. తరువాత ప్రతి variable ను type() లో పంపించి దాని data type ను తెలుసుకున్నాము.
Variable Type ను Message తో చూపించడం
score = 95
print("The type of score is:", type(score))
score variable లో 95 అనే పూర్తి సంఖ్య ఉంది. type(score) ఆ value యొక్క type ను గుర్తిస్తుంది. print() message మరియు type రెండింటినీ చూపిస్తుంది.
The type of score is:
Line-by-Line Explanation
ముందుగా type(25) 25 ఏ data type కు చెందినదో గుర్తిస్తుంది. 25 పూర్తి సంఖ్య కాబట్టి int వస్తుంది. print() ఆ సమాచారాన్ని screen పై చూపిస్తుంది.
4.5 decimal value. type() దీనిని float గా గుర్తిస్తుంది, print() ఆ type ను చూపిస్తుంది.
Python అనే text quotes లో ఉంది. కాబట్టి type() దీనిని str, అంటే string, గా గుర్తిస్తుంది.
True ఒక boolean value. type() దాని type ను bool గా గుర్తించి print() ద్వారా చూపిస్తుంది.
Ravi అనే text ను name variable లో నిల్వ చేస్తుంది. ఈ value యొక్క type str.
20 అనే పూర్తి సంఖ్యను age variable లో నిల్వ చేస్తుంది. ఈ value యొక్క type int.
5.8 అనే decimal సంఖ్యను height variable లో నిల్వ చేస్తుంది. దీని type float.
True అనే boolean value ను is_student variable లో నిల్వ చేస్తుంది. దీని type bool.
name లో ఉన్న Ravi value యొక్క type ను type() గుర్తిస్తుంది. ఆ type str కాబట్టి print() దాన్ని చూపిస్తుంది.
age లో ఉన్న 20 value యొక్క type ను type() గుర్తిస్తుంది. 20 పూర్తి సంఖ్య కాబట్టి output int అవుతుంది.
height లో ఉన్న 5.8 value యొక్క type ను type() గుర్తిస్తుంది. Decimal value కావడంతో output float అవుతుంది.
is_student లో ఉన్న True value యొక్క type ను type() గుర్తిస్తుంది. True boolean value కాబట్టి output bool అవుతుంది.
95 అనే పూర్తి సంఖ్యను score variable లో ఉంచుతుంది. అందువల్ల score యొక్క type int.
ఈ line message ను మరియు score యొక్క type ను print చేస్తుంది. type(score) int ను return చేస్తుంది, కాబట్టి పూర్తి output లో int కనిపిస్తుంది.
Try It
Try this Python code:
value = 42
print(type(value))
Practice
Practice: city అనే variable లో ఒక city name, score అనే variable లో ఒక పూర్తి సంఖ్య, is_student అనే variable లో True లేదా False ఉంచండి. తరువాత మూడు variables యొక్క types ను print చేయండి.
Hint: Text కోసం quotes ఉపయోగించండి. ప్రతి variable ను type() లో పెట్టి print() చేయండి.
Practice: price = 99.50 మరియు product = “Book” అనే variables సృష్టించి, వాటి types ను చూపించండి.
Hint: 99.50 యొక్క type float, Book యొక్క type str అవుతుంది.
Common Mistakes
ఏ value యొక్క type తెలుసుకోవాలనుకుంటున్నారో brackets లో ఇవ్వాలి: print(type(value)).
Text ను quotes లో రాయాలి: type(“Python”).
type() value ను మార్చదు. అది ఆ value యొక్క data type information ను మాత్రమే చూపిస్తుంది.
10 ఒక int, కానీ “10” ఒక str. Quotes ఉన్నాయా లేదా అనేది type ను ప్రభావితం చేస్తుంది.
Python case-sensitive కాబట్టి function పేరును చిన్న అక్షరాలతో సరిగ్గా type() గా రాయాలి.
Interview Questions
Python లో type() అంటే ఏమిటి?
Answer: type() ఒక value లేదా variable యొక్క data type ను తెలుసుకోవడానికి ఉపయోగించే built-in function.
print(type(10)) output ఏమిటి?
Answer: దాని output . ఎందుకంటే 10 ఒక integer.
type(“10”) మరియు type(10) మధ్య తేడా ఏమిటి?
Answer: type(“10”) str ను ఇస్తుంది, ఎందుకంటే అది text. type(10) int ను ఇస్తుంది, ఎందుకంటే అది పూర్తి సంఖ్య.
type() value ను మార్చుతుందా?
Answer: లేదు. type() value యొక్క data type ను మాత్రమే గుర్తిస్తుంది; value ను మార్చదు.
Exam Questions
క్రింది code యొక్క output ఏమిటి? print(type(3.14))
Answer:
క్రింది code యొక్క output ఏమిటి? print(type(False))
Answer:
ఒక variable యొక్క type తెలుసుకోవడానికి ఏ function ఉపయోగిస్తారు?
Answer:
type() function ఉపయోగిస్తారు.
x = “25” అయితే x యొక్క type ఏమిటి?
Answer:
x యొక్క type str, ఎందుకంటే 25 quotes లో ఉంది.
Quick Revision
- type() ఒక value లేదా variable యొక్క data type ను చూపిస్తుంది.
- Syntax: type(value)
- పూర్తి సంఖ్యలకు int, decimal సంఖ్యలకు float వస్తాయి.
- Text values కు str వస్తుంది.
- True మరియు False values కు bool వస్తుంది.
- type() value ను మార్చదు; type information ను మాత్రమే ఇస్తుంది.
- Quotes ఉన్న value మరియు quotes లేని number వేర్వేరు types కలిగి ఉంటాయి.
