Python Data Types
Python Data Types అంటే ఏమిటి?
Python లో ప్రతి value కి ఒక data type ఉంటుంది. Data type అంటే ఆ value ఏ రకానికి చెందినదో తెలిపే సమాచారం.
ఉదాహరణకు, 25 ఒక integer, 12.5 ఒక float, "Python" ఒక string, మరియు True ఒక boolean value.
Data Types ఎందుకు ఉపయోగిస్తాము?
Python ఒక value తో ఏ operations చేయవచ్చో data type ఆధారంగా నిర్ణయిస్తుంది. ఉదాహరణకు, numbers ను add చేయవచ్చు. Strings ను కలిపి text తయారు చేయవచ్చు. List లో అనేక values ను store చేయవచ్చు.
ప్రధాన Python Data Types
- int: పూర్తి సంఖ్యలు. ఉదాహరణ:
10,-5 - float: decimal సంఖ్యలు. ఉదాహరణ:
3.14,99.5 - complex: complex numbers. ఉదాహరణ:
2 + 3j - str: text లేదా characters. ఉదాహరణ:
"Hello" - bool: True లేదా False values
- list: మార్చగల values సమూహం. ఉదాహరణ:
[10, 20, 30] - tuple: మార్చలేని values సమూహం. ఉదాహరణ:
(10, 20, 30) - set: unique values సమూహం. ఉదాహరణ:
{1, 2, 3} - dict: key-value pairs ను store చేసే data type
- NoneType: value లేదని సూచించే
None
Value యొక్క type తెలుసుకోవడం
ఒక value ఏ data type కు చెందినదో తెలుసుకోవడానికి Python లో type() function ను ఉపయోగిస్తాము.
Python Dynamic Typing
Python లో variable ను declare చేసేటప్పుడు data type ను ముందుగా రాయాల్సిన అవసరం లేదు. Value ను assign చేసినప్పుడు Python స్వయంగా type ను గుర్తిస్తుంది. అందువల్ల Python ను dynamically typed language అంటారు.
Data Type మార్చడం
కొన్ని సందర్భాల్లో ఒక data type ను మరొక data type గా మార్చాలి. దీనిని type conversion అంటారు. ఉదాహరణకు int("25") string ను integer గా మార్చుతుంది. float(10) integer ను float గా మార్చుతుంది. Conversion చేసే ముందు value సరైన format లో ఉందో చూసుకోవాలి.
Simple Explanation
ఒక ఇంట్లో పుస్తకాలు, బట్టలు, ఆహారం వేర్వేరు రకాల వస్తువులుగా ఉంటాయి. అలాగే Python లో values కూడా వేర్వేరు data types గా ఉంటాయి.
int పూర్తి సంఖ్యలను, float decimal సంఖ్యలను, str text ను, bool నిజం లేదా తప్పు సమాచారాన్ని సూచిస్తుంది. అనేక values ను ఒకచోట ఉంచడానికి list, tuple, set, dict వంటి types ఉపయోగిస్తాము.
Python ఒక variable లో ఉన్న value ను చూసి దాని type ను స్వయంగా గుర్తిస్తుంది. type(value) ద్వారా ఆ type ను పరిశీలించవచ్చు.
Real-Life Example
ఒక విద్యార్థి వివరాలను program లో store చేస్తున్నామని అనుకోండి. విద్యార్థి పేరు str, వయస్సు int, మార్కుల average float, పరీక్షలో ఉత్తీర్ణుడా కాదా అనే సమాచారం bool అవుతుంది. ఈ values అన్నీ ఒకే రకమైనవి కావు కాబట్టి వాటికి సరైన data type అవసరం.
Code Examples
ప్రాథమిక Data Types గుర్తించడం
age = 20
price = 99.5
name = "Ravi"
is_student = True
print(type(age))
print(type(price))
print(type(name))
print(type(is_student))
ఈ code లో integer, float, string మరియు boolean values ను variables లో store చేశాము. type() function ప్రతి variable యొక్క data type ను చూపిస్తుంది.
Collection Data Types మరియు None
marks = [80, 90, 75]
point = (10, 20)
student = {"name": "Ravi", "age": 20}
result = None
print(type(marks))
print(type(point))
print(type(student))
print(type(result))
ఈ code లో list, tuple, dictionary మరియు None value ను చూపించాము. ప్రతి collection type కు data store చేసే విధానం వేర్వేరుగా ఉంటుంది.
Line-by-Line Explanation
20 అనే పూర్తి సంఖ్యను age variable లో store చేస్తుంది. Python దీనిని int type గా గుర్తిస్తుంది.
99.5 decimal value ను price variable లో store చేస్తుంది. ఈ value యొక్క type float.
"Ravi" అనే text ను name variable లో store చేస్తుంది. Text values యొక్క type str.
True అనే logical value ను is_student variable లో store చేస్తుంది. దీని type bool.
type(age) ద్వారా age యొక్క data type ను తెలుసుకుంటుంది. print() ఆ type ను screen పై చూపిస్తుంది.
మూడు marks ను ఒక list లో store చేస్తుంది. List లో values ను వరుసగా ఉంచవచ్చు.
రెండు values ను tuple లో store చేస్తుంది. Tuple values ను సాధారణంగా మార్చకుండా ఉంచడానికి ఉపయోగిస్తారు.
Student వివరాలను key-value pairs గా dictionary లో store చేస్తుంది. name మరియు age keys కు values ఉన్నాయి.
ప్రస్తుతం ఎలాంటి value లేదని సూచించడానికి None ను store చేస్తుంది. దీని type NoneType.
marks variable ఒక list అని type() ద్వారా తెలుసుకుని, ఆ సమాచారాన్ని print చేస్తుంది.
Try It
Try this Python code:
name = "Ravi"
age = 20
height = 5.8
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Practice
Practice:
మూడు variables సృష్టించండి: ఒక integer, ఒక float మరియు ఒక string. వాటి data types ను type() మరియు print() ఉపయోగించి చూపించండి.
Hint:
Integer కోసం 10, float కోసం 4.5, string కోసం "Python" వంటి values ఉపయోగించండి.
Practice:
is_active అనే variable లో True ను, items అనే variable లో మూడు values ఉన్న list ను store చేయండి. రెండు variables యొక్క types ను print చేయండి.
Hint:
Boolean value కోసం True, list కోసం square brackets [] ఉపయోగించండి.
Common Mistakes
String value ను quotes లేకుండా రాయడం, ఉదాహరణకు name = Ravi.
Text value ను single quotes లేదా double quotes లో రాయాలి. ఉదాహరణ: name = "Ravi".
Boolean value ను true లేదా false గా lowercase లో రాయడం.
Python లో boolean values తప్పనిసరిగా capital letter తో True మరియు False గా రాయాలి.
type ను function లాగా ఉపయోగించేటప్పుడు parentheses మర్చిపోవడం, ఉదాహరణకు type age.
type() function కు variable ను parentheses లో ఇవ్వాలి. ఉదాహరణ: type(age).
Number ను string గా రాసి కూడా దానిని integer అనుకోవడం, ఉదాహరణకు age = "20".
Quotes లో ఉన్న "20" ఒక string. సంఖ్యగా కావాలంటే age = 20 అని quotes లేకుండా రాయాలి.
List, tuple మరియు dictionary syntax ను కలగలపడం.
List కు [], tuple కు (), dictionary కు {} మరియు key-value pairs ఉపయోగిస్తారని గుర్తుంచుకోండి.
Interview Questions
Python data type అంటే ఏమిటి?
Answer: ఒక value ఏ రకానికి చెందినదో తెలిపేది data type. ఉదాహరణకు 10 యొక్క type int, “Hi” యొక్క type str.
Python లో ఒక value యొక్క data type ను ఎలా తెలుసుకోవచ్చు?
Answer: type() function ఉపయోగించాలి. ఉదాహరణ: type(value).
Python dynamically typed language అని ఎందుకు అంటారు?
Answer: Variable ను declare చేసేటప్పుడు data type ను ప్రత్యేకంగా రాయాల్సిన అవసరం లేదు. Value ను చూసి Python స్వయంగా type ను నిర్ణయిస్తుంది.
int మరియు float మధ్య తేడా ఏమిటి?
Answer: int పూర్తి సంఖ్యలను సూచిస్తుంది, ఉదాహరణకు 10. float decimal సంఖ్యలను సూచిస్తుంది, ఉదాహరణకు 10.5.
list మరియు tuple మధ్య ప్రాథమిక తేడా ఏమిటి?
Answer: List సాధారణంగా మార్చగల collection. Tuple సాధారణంగా మార్చలేని collection.
Exam Questions
క్రింది values యొక్క data types రాయండి: 25, 3.14, “Hello”, False.
Answer:
25 – int, 3.14 – float, “Hello” – str, False – bool.
type() function యొక్క ఉపయోగం ఏమిటి?
Answer:
ఒక value లేదా variable యొక్క data type ను తెలుసుకోవడానికి ఉపయోగిస్తాము.
Python లో text data కోసం ఏ data type ఉపయోగిస్తారు?
Answer:
Text data కోసం str లేదా string data type ఉపయోగిస్తారు.
Python లో value లేని స్థితిని సూచించడానికి ఏ value ఉపయోగిస్తారు?
Answer:
None ఉపయోగిస్తారు. దీని data type NoneType.
List మరియు dictionary ను ఒక్కొక్క ఉదాహరణతో రాయండి.
Answer:
List: [10, 20, 30]. Dictionary: {"name": "Ravi", "age": 20}.
Quick Revision
- Data type ఒక value యొక్క రకాన్ని సూచిస్తుంది.
intపూర్తి సంఖ్యలకు,floatdecimal సంఖ్యలకు ఉపయోగిస్తారు.strtext కోసం,boolTrue లేదా False కోసం ఉపయోగిస్తారు.list,tuple,set,dictcollections కోసం ఉపయోగించే data types.type()function తో value యొక్క data type తెలుసుకోవచ్చు.- Python variable యొక్క type ను value ఆధారంగా స్వయంగా గుర్తిస్తుంది.
Nonevalue లేకపోవడాన్ని సూచిస్తుంది.- Quotes లో ఉన్న సంఖ్య string; quotes లేకుండా ఉన్న సంఖ్య integer లేదా float కావచ్చు.
