Constants

Constants అంటే ఏమిటి?

Program అమలవుతున్న సమయంలో విలువను మార్చకూడదని భావించే పేరున్న విలువలను Constants అంటారు.

ఉదాహరణకు, ఒక వారంలో రోజులు ఎప్పుడూ 7. Pi విలువ సాధారణంగా 3.14159గా ఉపయోగించవచ్చు. ఇలాంటి విలువలను constant పేర్లలో ఉంచితే code సులభంగా చదవవచ్చు.

Pythonలో Constants ఎలా రాస్తారు?

Pythonలో constants కోసం ప్రత్యేకమైన keyword లేదు. సాధారణంగా constant పేరును పూర్తిగా capital lettersలో రాయడం ఒక coding convention.

PI = 3.14159
DAYS_IN_WEEK = 7

ఇక్కడ PI మరియు DAYS_IN_WEEK constant valuesగా భావించాలి. Python వాటిని నిజంగా మార్చకుండా ఆపదు. కానీ programmerగా మనం వాటి విలువను మార్చకూడదని పాటించాలి.

Constants ఎందుకు ఉపయోగిస్తారు?

  • మార్చకూడని విలువలకు అర్థవంతమైన పేరు ఇవ్వడానికి.
  • ఒకే విలువను programలో అనేక చోట్ల ఉపయోగించడానికి.
  • Codeను సులభంగా చదవడానికి.
  • విలువ మార్చాల్సి వస్తే ఒకే చోట మార్చడానికి.

ఎక్కడ ఉపయోగిస్తారు?

Tax rate, Pi విలువ, ఒక వారంలోని రోజులు, గరిష్ఠ స్కోర్, conversion values వంటి సందర్భాల్లో constants ఉపయోగపడతాయి.

Simple Explanation

Constant అనేది సాధారణంగా మారకూడని value. Pythonలో constantను సృష్టించడానికి ప్రత్యేక syntax లేదు. Variableలా value assign చేస్తాం, కానీ పేరును capital lettersలో రాస్తాం.

ఉదాహరణకు DAYS_IN_WEEK = 7. ఈ పేరును చూసిన వెంటనే ఈ value programలో మారకూడదని అర్థమవుతుంది. Pythonలో దాన్ని మళ్లీ assign చేయడం technically సాధ్యమే, కానీ అది మంచి పద్ధతి కాదు.

Real-Life Example

ఒక classroomలో ఒక వారానికి 7 రోజులు ఉంటాయి. ఈ సంఖ్యను programలో పలుమార్లు ఉపయోగించాల్సి వస్తే ప్రతి చోట 7 అని రాయడం కంటే DAYS_IN_WEEK అనే constantను ఉపయోగించడం మంచిది. పేరు చూసిన వెంటనే ఆ సంఖ్య ఏదో అర్థమవుతుంది.

Code Examples

Constants ఉపయోగించి Circle Area లెక్కించడం

PI = 3.14159
DAYS_IN_WEEK = 7
radius = 5
area = PI * radius * radius
print("Area:", area)
print("Days in a week:", DAYS_IN_WEEK)

ఈ exampleలో PI మరియు DAYS_IN_WEEK అనే constant valuesను సృష్టించాం. తరువాత PIను ఉపయోగించి radius ఆధారంగా circle areaను లెక్కించాం. Constant పేర్లు capital lettersలో ఉండటం వల్ల వాటిని మార్చకూడదని code చదివే వ్యక్తికి తెలుస్తుంది.

Area: 78.53975
Days in a week: 7

Line-by-Line Explanation

PI = 3.14159
ఈ lineలో Pi విలువను PI అనే పేరుతో store చేస్తున్నాం. పేరు capital lettersలో ఉండటం వల్ల ఇది constantగా ఉపయోగించాలనే ఉద్దేశం తెలుస్తుంది.
DAYS_IN_WEEK = 7
ఈ lineలో ఒక వారంలోని రోజుల సంఖ్యను DAYS_IN_WEEK అనే constantలో store చేస్తున్నాం. Programలో ఈ విలువను అవసరమైనప్పుడు పేరుతో ఉపయోగించవచ్చు.
radius = 5
ఈ lineలో circle యొక్క radiusను 5గా store చేస్తున్నాం. ఇది సాధారణ variable కాబట్టి lowercase పేరు ఉపయోగించాం.
area = PI * radius * radius
ఈ lineలో circle area సూత్రం ఉపయోగించి areaను లెక్కిస్తున్నాం. Python, PIలోని 3.14159 విలువను మరియు radiusలోని 5 విలువను ఉపయోగించి ఫలితాన్ని areaలో store చేస్తుంది.
print(“Area:”, area)
ఈ line area labelతో పాటు areaలో ఉన్న లెక్కించిన విలువను screenపై చూపిస్తుంది.
print(“Days in a week:”, DAYS_IN_WEEK)
ఈ line DAYS_IN_WEEK constantలో ఉన్న 7 విలువను labelతో కలిసి screenపై చూపిస్తుంది.

Try It

Try this Python code:

Example

TAX_RATE = 0.18
price = 1000
tax = price * TAX_RATE
total = price + tax
print("Tax:", tax)
print("Total:", total)

Try It Yourself »

Practice

Practice: ఒక college పేరు మరియు ఒక వారంలోని రోజుల సంఖ్య కోసం రెండు constants సృష్టించండి. College పేరును COLLEGE_NAMEగా, రోజుల సంఖ్యను DAYS_IN_WEEKగా రాయండి. తరువాత రెండు valuesను print చేయండి.

Hint: Constant పేర్లను capital lettersలో రాయండి. ఉదాహరణకు COLLEGE_NAME = “Green Valley College”.

Practice: TAX_RATE అనే constantను 0.18గా సృష్టించండి. price అనే variableను 2000గా ఉంచి, taxను లెక్కించి print చేయండి.

Hint: taxను లెక్కించడానికి price * TAX_RATE ఉపయోగించండి.

Common Mistakes

Constant పేరును lowercaseలో రాయడం, ఉదాహరణకు pi = 3.14159.
Constant కోసం సాధారణ convention ప్రకారం పేరు capital lettersలో రాయండి: PI = 3.14159.
Constant పేరులో spaces ఉపయోగించడం, ఉదాహరణకు DAYS IN WEEK = 7.
Spaces బదులుగా underscore ఉపయోగించండి: DAYS_IN_WEEK = 7.
Python constant valueను automatically మార్చకుండా ఆపుతుందని అనుకోవడం.
Pythonలో constants కోసం ప్రత్యేక restriction లేదు. Capital letters ఒక convention మాత్రమే. Programmerగా constant valueను మళ్లీ మార్చకుండా జాగ్రత్తపడాలి.
Constant పేరును ప్రారంభంలో digitతో రాయడం, ఉదాహరణకు 7_DAYS = 7.
Variable naming rules పాటించండి. పేరును letter లేదా underscoreతో ప్రారంభించండి: DAYS_IN_WEEK = 7.

Interview Questions

Pythonలో constant అంటే ఏమిటి?

Answer: Programలో విలువ మార్చకూడదని భావించే పేరున్న valueను constant అంటారు.

Pythonలో constants కోసం ప్రత్యేక keyword ఉందా?

Answer: లేదు. Pythonలో constants కోసం ప్రత్యేక keyword లేదు. సాధారణంగా constant పేర్లను capital lettersలో రాస్తారు.

Pythonలో constant valueను మార్చగలమా?

Answer: అవును, Python technically valueను మళ్లీ assign చేయడానికి అనుమతిస్తుంది. కానీ coding convention ప్రకారం constant valueను మార్చకూడదు.

Constantsను capital lettersలో ఎందుకు రాస్తారు?

Answer: ఆ valueను మార్చకూడదని, అది constant అని code చదివే వారికి స్పష్టంగా తెలియజేయడానికి capital letters ఉపయోగిస్తారు.

Exam Questions

క్రింది codeలో constant ఏది? PI = 3.14
radius = 2

Answer:

PI constant. ఎందుకంటే దాని పేరు capital lettersలో ఉంది మరియు అది సాధారణంగా మారని విలువగా ఉపయోగించబడుతుంది.

ఒక వారంలోని రోజుల సంఖ్యను constantగా ఎలా రాస్తారు?

Answer:

DAYS_IN_WEEK = 7

Pythonలో constantsను interpreter తప్పనిసరిగా మార్చకుండా ఆపుతుందా?

Answer:

లేదు. Pythonలో ఇది ఒక naming convention మాత్రమే; interpreter ప్రత్యేకంగా valueను lock చేయదు.

Constant పేరులో spaces ఉంటే సరైన రూపం ఏమిటి? DAYS IN WEEK = 7

Answer:

సరైన రూపం DAYS_IN_WEEK = 7. Spaces స్థానంలో underscores ఉపయోగించాలి.

Quick Revision

  • Constant అనేది సాధారణంగా మార్చకూడని value.
  • Pythonలో constants కోసం ప్రత్యేక keyword లేదు.
  • Constants పేర్లను సాధారణంగా capital lettersలో రాస్తారు.
  • ఒకే విలువను programలో పలుమార్లు ఉపయోగించడానికి constants ఉపయోగపడతాయి.
  • Constant పేరు కోసం spaces బదులుగా underscores ఉపయోగించాలి.
  • Python technically constantను మార్చడానికి అనుమతిస్తుంది, కాబట్టి programmer conventionను పాటించాలి.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top