Python Syntax

Python Syntax అంటే ఏమిటి?

Python Syntax అంటే Python code రాయడానికి పాటించాల్సిన rules. మనం ఒక భాషలో grammar పాటించినట్లే, Python లో కూడా సరైన syntax పాటించాలి.

Python Syntax ఎందుకు అవసరం?

Python మన code ను అర్థం చేసుకోవడానికి syntax అవసరం. Syntax తప్పుగా ఉంటే Python error చూపిస్తుంది. సరైన syntax తో రాసిన code మాత్రమే సరిగ్గా execute అవుతుంది.

Python Syntax ముఖ్యమైన నియమాలు

  • Python statements సాధారణంగా కొత్త line లో రాయాలి.
  • Variable name మరియు value మధ్య = ఉపయోగించాలి.
  • Text values ను quotes లో రాయాలి.
  • Code block ప్రారంభించడానికి : ఉపయోగించాలి.
  • Code block లోని statements ను indentation తో రాయాలి.
  • Python case-sensitive. అంటే Name మరియు name వేర్వేరు.

Syntax ఎలా పనిచేస్తుంది?

Python code ను line by line చదువుతుంది. ప్రతి line లోని words, symbols మరియు spaces కు ప్రత్యేకమైన అర్థం ఉంటుంది. ఉదాహరణకు, name = "Ravi" లో name ఒక variable, = assignment symbol, "Ravi" ఒక text value.

Indentation

Python లో indentation చాలా ముఖ్యమైనది. ఒక code block లో ఉన్న statement ముందు సాధారణంగా నాలుగు spaces వాడుతారు. ఇతర programming languages లో braces వాడే చోట Python indentation ను ఉపయోగిస్తుంది.

Syntax ఉపయోగించే ప్రదేశాలు

Variables, conditions, loops, functions వంటి ప్రతి Python code భాగంలో syntax rules ఉపయోగిస్తాం. ఈ lesson లో వాటిలోని basic syntax rules మాత్రమే నేర్చుకుంటాం.

Simple Explanation

సులభంగా చెప్పాలంటే, Python Syntax అంటే Python కి అర్థమయ్యే విధంగా code రాయే పద్ధతి. ప్రతి statement ను సరైన spelling, symbols, quotes మరియు indentation తో రాయాలి.

ఉదాహరణకు, age = 20 సరైన syntax. కానీ age 20 లో = లేకపోవడం వల్ల Python దాన్ని అర్థం చేసుకోదు.

అలాగే, ఒక line తర్వాత code block వస్తే : పెట్టాలి. ఆ block లోని code ను indentation తో రాయాలి.

Real-Life Example

ఒక recipe లో ingredients మరియు cooking steps ఒక నిర్దిష్ట క్రమంలో ఉంటాయి. ఆ క్రమం పాటించకపోతే సరైన dish తయారు కాదు. అదే విధంగా Python code లో syntax rules పాటించకపోతే program సరిగ్గా run కాదు.

ఉదాహరణకు, ఒక instruction మొదలైనప్పుడు colon పెట్టి, దాని లోపల ఉన్న steps ను కొద్దిగా ముందుకు జరిపి రాయడం Python indentation లాంటిది.

Code Examples

Basic Python Syntax

name = "Ravi"
age = 20
print(name)
print(age)

ఈ example లో variables కు values assign చేసి, వాటిని output గా చూపిస్తున్నాం. ప్రతి statement ను ప్రత్యేకమైన line లో రాశాం. Text value కోసం quotes ఉపయోగించాం.

Ravi
20

Colon and Indentation Syntax

age = 20
if age >= 18:
    print("Adult")

ఈ example లో if statement తర్వాత colon పెట్టాం. Condition true అయితే indentation తో ఉన్న print() statement execute అవుతుంది. ఇక్కడ indentation Python code block ను సూచిస్తుంది.

Adult

Line-by-Line Explanation

name = “Ravi”

ఈ line లో name అనే variable ను సృష్టించి, దానికి "Ravi" అనే text value ను assign చేస్తుంది. Python ఈ value ను name లో store చేస్తుంది.

age = 20

ఈ line లో age variable కు 20 అనే number value assign అవుతుంది. తర్వాత ఈ variable ను ఉపయోగించవచ్చు.

print(name)

ఈ line లో name variable లో ఉన్న Ravi value screen పై చూపబడుతుంది. Python ముందుగా variable value ను తీసుకుని output ఇస్తుంది.

print(age)

ఈ line లో age variable లో ఉన్న 20 value screen పై చూపబడుతుంది.

age = 20

ఈ line age variable లో 20 ను store చేస్తుంది. తరువాతి condition ఈ value ను ఉపయోగిస్తుంది.

if age >= 18:

ఈ line age విలువ 18 లేదా అంతకంటే ఎక్కువ ఉందా అని తనిఖీ చేస్తుంది. చివరలోని colon ఈ condition కు సంబంధించిన code block తరువాత వస్తుందని Python కు తెలియజేస్తుంది.

print(“Adult”)

ఈ line ముందు నాలుగు spaces ఉన్నాయి. అందువల్ల ఇది if code block లో భాగం. Condition true అయినప్పుడు Python ఈ line ను execute చేసి Adult ను చూపిస్తుంది.

Try It

Try this Python code:

Example

name = "Anu"
age = 15
print(name)
print(age)

Try It Yourself »

Practice

Practice: ఒక school name మరియు class number కోసం రెండు variables సృష్టించండి. వాటి values ను assign చేసి, రెండింటినీ output గా చూపించండి.

Hint: Text కోసం quotes ఉపయోగించండి. Variable assignment కోసం = ఉపయోగించండి. ప్రతి value ను print() తో చూపించండి.

Practice: క్రింది code లో syntax error ను గుర్తించి సరిచేయండి: student_name "Ravi"

Hint: Variable name మరియు value మధ్య assignment symbol అవసరం.

Practice: ఒక age variable ను 20 గా ఉంచి, age 18 లేదా అంతకంటే ఎక్కువ అయితే Adult అని చూపించే code రాయండి.

Hint: if statement తర్వాత colon పెట్టండి. print statement ను indentation తో రాయండి.

Common Mistakes

Variable name మరియు value మధ్య = symbol రాయకపోవడం. ఉదాహరణ: name "Ravi".

Assignment కోసం = symbol ఉపయోగించాలి: name = "Ravi".

Text value ను quotes లేకుండా రాయడం. ఉదాహరణ: name = Ravi.

Text ను single quotes లేదా double quotes లో రాయాలి: name = "Ravi".

if statement తర్వాత colon పెట్టకపోవడం.

Condition line చివరలో colon పెట్టాలి: if age >= 18:.

Code block లోని statement కు indentation ఇవ్వకపోవడం.

Code block లోని line ముందు సాధారణంగా నాలుగు spaces ఇవ్వాలి: print("Adult").

Capital letters మరియు small letters ను ఒకేలా భావించడం. ఉదాహరణకు Name మరియు name ను ఒకే variable గా ఉపయోగించడం.

Python case-sensitive అని గుర్తుంచుకోండి. Variable ను ఒకే spelling మరియు case తో ఉపయోగించండి.

Interview Questions

Python Syntax అంటే ఏమిటి?

Answer: Python code ను సరైన విధంగా రాయడానికి పాటించాల్సిన rules ను Python Syntax అంటారు.

Python లో indentation ఎందుకు ముఖ్యమైనది?

Answer: Indentation ద్వారా code block ఏ statements కు సంబంధించినదో Python గుర్తిస్తుంది.

Python case-sensitive అంటే ఏమిటి?

Answer: Capital letters మరియు small letters ను Python వేర్వేరు characters గా పరిగణిస్తుంది. name మరియు Name వేర్వేరు identifiers.

Python లో assignment కోసం ఏ symbol ఉపయోగిస్తారు?

Answer: Assignment కోసం = symbol ఉపయోగిస్తారు. ఉదాహరణ: age = 20.

Exam Questions

సరైన syntax ను ఎంచుకోండి: A) age 20 B) age = 20

Answer:

సరైన సమాధానం B: age = 20.

Python code block ప్రారంభమవుతుందని తెలియజేయడానికి ఏ symbol ఉపయోగిస్తారు?

Answer:

Colon symbol : ఉపయోగిస్తారు.

Python లో text value ను ఎలా రాస్తారు?

Answer:

Text value ను single quotes లేదా double quotes లో రాస్తారు. ఉదాహరణ: city = "Hyderabad".

క్రింది code లో indentation అవసరమయ్యే line ఏది? if age >= 18: మరియు print("Adult")

Answer:

print("Adult") line if block లో ఉంది కాబట్టి దానికి indentation అవసరం.

Quick Revision

  • Python Syntax అంటే Python code రాయడానికి పాటించాల్సిన rules.
  • ప్రతి statement ను సాధారణంగా కొత్త line లో రాయాలి.
  • Variable కు value assign చేయడానికి = symbol ఉపయోగించాలి.
  • Text values ను quotes లో రాయాలి.
  • if వంటి code block statements తర్వాత colon ఉపయోగించాలి.
  • Code block లోని statements కు indentation ఇవ్వాలి.
  • Python case-sensitive language.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top