Python Data types: Text(str), Numeric(int, float, complex), Boolean(bool)

Python માં દરેક value નો એક type હોય છે, text માટે str, numbers માટે int અને float અને complex, true કે false માટે bool, અને type નક્કી કરે છે કે value શું કરી શકે.

9 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

એ જ 30, ત્રણ અલગ અર્થ

તમારા PocketMoney tracker માં, સંખ્યા 30 ત્રણ અલગ વસ્તુઓનો અર્થ હોઈ શકે: તમે ખર્ચેલા પૂરા rupees (30), paise સાથે એક સચોટ amount (30.50), કે બસ 'શું મેં વધુ ખર્ચ્યું?' નો જવાબ (ના).

Python ને જાણવું જરૂરી છે કે એ કયા પ્રકારની value રાખી રહ્યું છે, કારણ કે તમે દરેક એક સાથે બહુ અલગ વસ્તુઓ કરી શકો છો. તમે amounts નો એક decimal average લઈ શકો, પણ 'શું મેં વધુ ખર્ચ્યું' માત્ર true કે false હોઈ શકે.

એ 'કયો પ્રકાર' value નો data type છે, અને Python પાસે મૂળ types નો એક નાનો set છે. એમને મળો અને તમે લગભગ કોઈ પણ Python program વાંચી શકો છો.

Theory

Types container નો આકાર છે

values ને liquids અને types ને એ containers ની જેમ વિચારો જેમાં એ આવે છે: સચોટ decimals માટે એક measuring jug, પૂરા counts માટે એક tally counter, text માટે એક name tag, એક સરળ yes/no switch. તમે એક light switch થી વરસાદ નહીં માપો. Python માં એક value નો type નક્કી કરે છે કે કયા operations અર્થપૂર્ણ છે, એ જ કારણ છે કે Python હંમેશા એને તમારા માટે track કરતું રહે છે.

Theory

મૂળ types

Python ના રોજિંદા built-in types:

  • str (text): quotes માં કંઈ પણ, જેમ "samosa".
  • int (whole numbers): 30, -5, 2026. કોઈ decimal point નહીં.
  • float (decimals): 30.5, 0.0, 3.14. એક decimal point હોય છે.
  • complex: એક imaginary part વાળા numbers, 2 + 3j લખાય છે (Python j વાપરે છે, i નહીં).
  • bool (truth): બિલકુલ બે values, True અને False.

function type(value) તમને કહે છે કે તમે કઈ રાખી રહ્યા છો, checking માટે તમારું મુખ્ય tool.

At a glance

પાંચ મૂળ types. str, int, float, અને bool રોજના ચાર છે; complex વિશેષ છે.

Typeઉદાહરણ valuePocketMoney માં શેના માટે વપરાય
str"samosa"એક entry માં item નું નામ
int30ખર્ચેલા પૂરા rupees, એક day counter
float30.50paise સાથે એક સચોટ amount
complex2 + 3jaccounts માં દુર્લભ; engineering maths માં વપરાય
boolTrue'શું budget પાર થયું છે?' નો જવાબ

Practical

type() થી types તપાસવા

balance = 500          # int
amount = 30.50         # float
item = "samosa"        # str
over_budget = False    # bool

print(type(balance))     # <class 'int'>
print(type(amount))      # <class 'float'>
print(type(item))        # <class 'str'>
print(type(over_budget)) # <class 'bool'>

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

int કે float?

tap કરતાં પહેલાં: 10 નો type શું છે અને 10.0 નો type શું છે? શું એ એકસરખા છે?

Show the answer

10 એક int છે અને 10.0 એક float છે: એકસરખો type નહીં, ભલે એ સંખ્યાત્મક રીતે બરાબર છે. decimal point પૂરી વાર્તા છે: એ એક value ને એક float તરીકે mark કરે છે. exams માં આ મહત્ત્વનું છે, type(10) int છે, type(10.0) float છે, અને Python માં / થી ભાગ હંમેશા એક float આપે છે (તો 10 / 2 5.0 છે, 5 નહીં).

Theory

bool વિશે આશ્ચર્ય

bool ગુપ્ત રીતે એક પ્રકારનું int છે. Python માં, True 1 ની જેમ વર્તે છે અને False 0 ની જેમ.

એ એક યુક્તિ નથી જેના પર તમારે સ્પષ્ટતા માટે આધાર રાખવો જોઈએ, પણ એ ખરું behaviour સમજાવે છે: True + True 2 છે, અને True == 1 True છે.

એ કેમ મહત્ત્વનું છે? કારણ કે balance < 100 જેવી એક comparison અસ્પષ્ટ 'yes/no' text બનાવતી નથી, એ એક ખરી bool value બનાવે છે જેને તમે store, print, કે તો count પણ કરી શકો. એ દરેક if decision નો આધાર છે જે તમે આગળના unit માં લખશો.

Quiz

Python માં, expression `True == 1` શેમાં evaluate થાય છે?

  1. True, કારણ કે bool int નો એક subtype છે જ્યાં True 1 બરાબર છે
  2. False, કારણ કે એક boolean ક્યારેય એક number બરાબર ન હોઈ શકે
  3. એ અલગ types ની સરખામણી માટે એક TypeError ફેંકે છે
  4. None, કારણ કે comparison અવ્યાખ્યાયિત છે
Show the answer

True, કારણ કે bool int નો એક subtype છે જ્યાં True 1 બરાબર છે

Python માં, bool int ના ઉપર બન્યું છે: True 1 છે અને False 0 છે, તો True == 1 True છે (અને False == 0 True છે). Python ખુશીથી એમની સરખામણી કરે છે કારણ કે એ એ જ અંતર્ગત પ્રકારના છે. આ એક પ્રિય exam catch છે: એ એક error ફેંકતું નથી અને None પાછું આપતું નથી.

Watch out

quotes એને text બનાવે છે

સૌથી મોટી beginner ભૂલ: "30" (quotes માં) એક str છે, એક number નહીં. એ 30 જેવું દેખાય છે પણ તમે એના પર arithmetic કરી શકતા નથી, "30" + 5 એક TypeError છે, અને "30" + "30" "3030" આપે છે, 60 નહીં. જો એક value user input કે એક text file થી આવી છે, એ લગભગ હંમેશા એક str છે જ્યાં સુધી તમે એને convert ન કરો. એ conversion બિલકુલ આગળનો topic છે (type casting).

Theory

C થી એક મોટો બદલાવ

BCA104 માં તમે પહેલાં type declare કર્યો (int marks;) અને એ હંમેશા માટે નક્કી હતો. Python dynamically typed છે: તમે ક્યારેય એક type declare કરતા નથી, તમે જે value assign કરો છો એ એને નક્કી કરે છે, અને type() એને ઉજાગર કરે છે. એક નામ હવે એક int અને પછી એક str પણ રાખી શકે. સ્વતંત્રતા, પણ જવાબદારી પણ: કારણ કે Python declaration સમયે તમને ચેતવણી નહીં આપે, debugging કરતાં type() થી types તપાસવી એક ટેવ છે જે જાળવવા યોગ્ય છે.

Summary

Key takeaways

  • દરેક Python value નો એક type હોય છે; type(value) એને ઉજાગર કરે છે.
  • str quotes માં text છે; int whole numbers છે; float માં એક decimal point છે; complex imaginary part માટે j વાપરે છે.
  • bool ની બે values છે, True અને False, અને એ int નો એક subtype છે (True == 1, False == 0).
  • 10 (int) અને 10.0 (float) value માં બરાબર છે પણ type માં અલગ; / હંમેશા એક float આપે છે.
  • quotes માં "30" એક str છે, એક number નહીં: એના પર arithmetic નિષ્ફળ થાય કે concatenate કરે છે.
  • Memory hook: quotes એટલે text, એક dot એટલે float, કોઈ dot નહીં એટલે int, અને True બસ એક શણગારેલો 1 છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Fundamentals

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Python Data types: Text(str), Numeric(int, float, complex), Boolean(bool) · Programming Skills · Gri-Learn