Arithmetic Operators
Arithmetic Operators అంటే ఏమిటి?
Pythonలో సంఖ్యలపై గణిత లెక్కలు చేయడానికి ఉపయోగించే Operatorsను Arithmetic Operators అంటారు. Addition, subtraction, multiplication వంటి లెక్కలను Pythonలో సులభంగా చేయడానికి ఇవి ఉపయోగపడతాయి.
Arithmetic Operators రకాలు
- + Addition: రెండు విలువలను కలుపుతుంది.
- – Subtraction: ఒక విలువను మరొక విలువ నుంచి తీసివేస్తుంది.
- * Multiplication: రెండు విలువలను గుణిస్తుంది.
- / Division: భాగహారం చేస్తుంది మరియు సాధారణంగా decimal ఫలితాన్ని ఇస్తుంది.
- % Modulus: భాగించినప్పుడు మిగిలే remainderను ఇస్తుంది.
- // Floor Division: భాగహారం యొక్క పూర్తి సంఖ్య భాగాన్ని ఇస్తుంది.
- ** Exponentiation: ఒక సంఖ్యను మరొక సంఖ్య powerకు పెంచుతుంది.
ఎందుకు ఉపయోగిస్తాము?
సంఖ్యలతో పనిచేసే ప్రతి programలో Arithmetic Operators ఉపయోగపడతాయి. Shopping bill, marks calculation, salary calculation, distance calculation, time conversion వంటి పనుల్లో ఇవి అవసరం.
Operator Precedence
ఒకే expressionలో అనేక operators ఉన్నప్పుడు Python ఒక క్రమంలో వాటిని execute చేస్తుంది. సాధారణంగా parentheses ముందు, తరువాత power, తరువాత multiplication లేదా division, చివరగా addition లేదా subtraction జరుగుతాయి.
ఉదాహరణకు 2 + 3 * 4 లో ముందుగా multiplication జరుగుతుంది. అందువల్ల ఫలితం 14. ముందుగా addition చేయాలంటే (2 + 3) * 4 అని రాయాలి.
Simple Explanation
Arithmetic Operatorsను calculatorలోని buttonsలా ఊహించుకోవచ్చు. + కలుపుతుంది, - తీసివేస్తుంది, * గుణిస్తుంది, / భాగిస్తుంది.
% operator పూర్తిగా భాగించలేని సమయంలో మిగిలే సంఖ్యను చూపిస్తుంది. ఉదాహరణకు 10 % 3 ఫలితం 1, ఎందుకంటే 10ను 3తో భాగించినప్పుడు remainder 1.
// పూర్తి సంఖ్య భాగాన్ని మాత్రమే ఇస్తుంది. 10 // 3 ఫలితం 3. ** power కోసం ఉపయోగిస్తాము. 2 ** 3 అంటే 2 × 2 × 2, కాబట్టి ఫలితం 8.
Real-Life Example
ఒక షాపులో ఒక notebook ధర 50 రూపాయలు, మీరు 3 notebooks కొనుగోలు చేశారని అనుకుందాం. మొత్తం ధర తెలుసుకోవడానికి 50 * 3 ఉపయోగించవచ్చు. మీరు 200 రూపాయలు ఇచ్చినప్పుడు మిగిలే change తెలుసుకోవడానికి 200 - 150 ఉపయోగించవచ్చు. ఇలాంటి లెక్కలన్నింటికీ Arithmetic Operators ఉపయోగపడతాయి.
Code Examples
ప్రాథమిక Arithmetic Operators
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
print(a // b)
print(a ** b)
ఈ code రెండు సంఖ్యలను తీసుకుని అన్ని ముఖ్యమైన Arithmetic Operatorsను చూపిస్తుంది. Division decimal ఫలితాన్ని ఇస్తుంది, Modulus remainderను ఇస్తుంది, Floor Division పూర్తి సంఖ్య భాగాన్ని ఇస్తుంది, Exponentiation power ఫలితాన్ని ఇస్తుంది.
13 7 30 3.3333333333333335 1 3 1000
Operator Precedence మరియు Parentheses
result1 = 2 + 3 * 4
result2 = (2 + 3) * 4
print(result1)
print(result2)
మొదటి expressionలో multiplication ముందుగా జరుగుతుంది. రెండవ expressionలో parenthesesలో ఉన్న addition ముందుగా జరుగుతుంది. అందువల్ల రెండు expressionsకు వేర్వేరు ఫలితాలు వస్తాయి.
14 20
Line-by-Line Explanation
ఈ lineలో 10 అనే సంఖ్యను a variableలో నిల్వ చేస్తుంది. తరువాత arithmetic calculationలో aను ఉపయోగించవచ్చు.
ఈ lineలో 3 అనే సంఖ్యను b variableలో నిల్వ చేస్తుంది. Python ఈ విలువను తరువాతి expressionsలో ఉపయోగిస్తుంది.
a మరియు b విలువలను కలిపి 13ను print చేస్తుంది.
a నుంచి bను తీసివేసి 7ను print చేస్తుంది.
a మరియు bను గుణించి 30ను print చేస్తుంది.
aను bతో భాగిస్తుంది. Python సాధారణ divisionలో decimal ఫలితాన్ని print చేస్తుంది.
aను bతో భాగించినప్పుడు మిగిలే remainder అయిన 1ను print చేస్తుంది.
Floor Division ఉపయోగించి భాగహారం యొక్క పూర్తి సంఖ్య భాగమైన 3ను print చేస్తుంది.
aను b powerకు పెంచుతుంది. అంటే 10³, కాబట్టి ఫలితం 1000.
ఈ expressionలో multiplicationకు ఎక్కువ precedence ఉండటం వల్ల ముందుగా 3 × 4 జరుగుతుంది. తరువాత 2ను కలిపి 14ను result1లో నిల్వ చేస్తుంది.
Parenthesesలో ఉన్న addition ముందుగా జరుగుతుంది. 2 + 3 ఫలితం 5, తరువాత 5 × 4 చేసి 20ను result2లో నిల్వ చేస్తుంది.
result1లో ఉన్న 14ను outputగా చూపిస్తుంది.
result2లో ఉన్న 20ను outputగా చూపిస్తుంది.
Try It
Try this Python code:
price = 50
quantity = 4
total = price * quantity
print(total)
Practice
Practice: ఒక rectangle యొక్క length 8 మరియు width 5. దాని area మరియు perimeterను Arithmetic Operators ఉపయోగించి calculate చేసి print చేయండి.
Hint: Area = length × width. Perimeter = 2 × (length + width). Parentheses ఉపయోగించండి.
Practice: ఒక వస్తువు ధర 75 మరియు quantity 6. మొత్తం ధరను calculate చేసి print చేయండి.
Hint: Total price కోసం priceను quantityతో multiply చేయండి.
Practice: 25ను 4తో భాగించినప్పుడు వచ్చే quotient మరియు remainderను print చేయండి.
Hint: Quotient కోసం //, remainder కోసం % ఉపయోగించండి.
Common Mistakes
Multiplication కోసం x లేదా × ఉపయోగించడం. Pythonలో ఇవి valid multiplication operators కావు.
Multiplication కోసం ఎల్లప్పుడూ * ఉపయోగించాలి. ఉదాహరణ: 5 * 2.
Divisionలో / మరియు // ఒకే ఫలితాన్ని ఇస్తాయని అనుకోవడం.
/ సాధారణంగా decimal ఫలితాన్ని ఇస్తుంది. // పూర్తి సంఖ్య భాగాన్ని మాత్రమే ఇస్తుంది.
Remainder కోసం / ఉపయోగించడం.
Remainder తెలుసుకోవడానికి % operator ఉపయోగించాలి. ఉదాహరణ: 10 % 3.
Operator precedenceను పట్టించుకోకుండా expression రాయడం.
Calculation క్రమం స్పష్టంగా ఉండాలంటే అవసరమైన చోట parentheses ఉపయోగించండి. ఉదాహరణ: (2 + 3) * 4.
Power కోసం ^ ఉపయోగించడం. Pythonలో ^ arithmetic power operator కాదు.
Power కోసం ** ఉపయోగించాలి. ఉదాహరణ: 2 ** 3.
Interview Questions
Pythonలో multiplication కోసం ఏ operator ఉపయోగిస్తారు?
Answer:
Multiplication కోసం * operator ఉపయోగిస్తారు. ఉదాహరణ: 4 * 5.
/ మరియు // మధ్య తేడా ఏమిటి?
Answer:
/ సాధారణ division చేసి decimal ఫలితాన్ని ఇవ్వవచ్చు. // floor division చేసి పూర్తి సంఖ్య భాగాన్ని ఇస్తుంది.
Pythonలో remainder తెలుసుకోవడానికి ఏ operator ఉపయోగిస్తారు?
Answer:
Remainder కోసం % operator ఉపయోగిస్తారు. ఉదాహరణకు 10 % 3 ఫలితం 1.
Pythonలో power calculation ఎలా చేస్తారు?
Answer:
** operator ఉపయోగిస్తారు. ఉదాహరణకు 2 ** 3 ఫలితం 8.
Exam Questions
print(15 + 5) output ఏమిటి?
Answer:
Output 20.
print(17 % 5) output ఏమిటి?
Answer:
Output 2, ఎందుకంటే 17ను 5తో భాగించినప్పుడు remainder 2.
print(17 // 5) output ఏమిటి?
Answer:
Output 3, ఎందుకంటే floor division పూర్తి సంఖ్య భాగాన్ని మాత్రమే ఇస్తుంది.
print(2 + 4 * 3) output ఏమిటి?
Answer:
Output 14. ముందుగా 4 × 3 = 12, తరువాత 2 + 12 = 14.
print((2 + 4) * 3) output ఏమిటి?
Answer:
Output 18. Parentheses వల్ల ముందుగా 2 + 4 = 6, తరువాత 6 × 3 = 18.
Quick Revision
- Arithmetic Operators సంఖ్యలపై గణిత లెక్కలు చేయడానికి ఉపయోగిస్తారు.
+addition,-subtraction,*multiplication కోసం ఉపయోగిస్తారు./సాధారణ division,//floor division కోసం ఉపయోగిస్తారు.%remainderను ఇస్తుంది.**power calculation కోసం ఉపయోగిస్తారు.- Parentheses ఉపయోగిస్తే calculation క్రమాన్ని మనం నియంత్రించవచ్చు.
- ఒక expressionలో multiplication మరియు division సాధారణంగా addition, subtraction కంటే ముందుగా execute అవుతాయి.
