Concepts / Data Types and Variables: Storing Information

Data Types and Variables: Storing Information

Names created for variables or functions also need consistent spelling.

  • Programming

A Language with Rules

When you communicate with another person, knowing individual words is not enough. You must also arrange those words into sentences according to grammatical rules. Python works in the same way. Its vocabulary includes words such as if, while, def, and return, along with names, operators, and literals. Its grammar determines how those pieces must be arranged into statements that Python can understand.

A Python statement succeeds only when both parts are correct: the words must be recognized and correctly spelled, and the words must appear in an arrangement allowed by Python's grammar. A misspelled word or a malformed arrangement prevents Python from understanding the instruction.

Python vocabularykeywords, names, operators,literalsValid statementPython can understandPython grammararrangement rulesSyntax errorPython refuses themalformed instructioncorrect wordscorrect arrangementmisspelled wordinvalid arrangement
How do Python words and the rules for arranging them work together to form a valid statement?

Spelling Is Part of Meaning

Python is case-sensitive. This means that uppercase and lowercase letters are not interchangeable: Print is different from print, and Score is different from score. Every character must match exactly when you use Python vocabulary or reuse a name you created.

Python has a fixed set of keywords, including if, for, while, def, and return. These words must be spelled precisely. A spelling such as iff is not recognized as the keyword if, and wile is not recognized as while. Names created for variables or functions also need consistent spelling. If a name is written as student_name in one place, studentname and student_Name are different names.

Before
student_name

A name created for a variable

After

student_Name

A differently spelled name

Building a Well-Formed Statement

Python statements follow specific patterns. An if statement uses the keyword if, then a condition, and then a colon. A function definition starts with def, then the function name, followed by parentheses and a colon. A function call includes parentheses. These punctuation marks and positions are part of Python's grammar, not optional decoration.

python

In this illustrative statement, if appears first, the condition follows it, and the line ends with a colon. The call to print includes parentheses. The example demonstrates the source-described shapes of an if statement and a function call; its particular condition and value are generated for teaching.

ifkeywordconditiontest to evaluate:required endingfollowed byfollowed by
Which parts of a Python statement fit together, and where must each part appear?

Tracing Errors to Their Cause

Worked example

Checking a Statement Before Running It

Decide whether the following illustrative statement follows the source-described pattern for an if statement: if ready

  1. 1
    Find the keyword

    The statement begins with if, which is the required keyword for this kind of statement.

  2. 2
    Find the condition

    The word ready is being used as the condition in this generated example.

  3. 3
    Check the ending

    The line does not end with a colon. The source states that an if statement must end with a colon.

  4. 4
    Classify the statement

    Because a required grammatical element is missing, the statement is invalid Python syntax.

Result

The missing colon causes a syntax error.

if ready:keyword, condition, colonif readycolon missingValid syntaxwell-formed statementSyntax errorstatement refusedmatches the patternbreaks the pattern
What changes between a correctly written Python statement and one that violates Python's grammar rules?

Syntax errors are not random obstacles. They identify a failure in the vocabulary or grammar of the instruction. The source examples describe several common causes: a missing colon after an if or while line, incorrect capitalization in a name such as My_name instead of my_name, and a missing closing parenthesis.

Mistakes Beginners Make

  • Common mistake

    Changing capitalization in a Python word or name

    What it looks like

    Using Print when the required word is print, or My_name when the existing name is my_name.

    Why it fails

    Python is case-sensitive and compares every character exactly.

    Do this instead

    Match the capitalization and spelling every time the word or name is used.

  • Common mistake

    Misspelling a keyword

    What it looks like

    Writing iff instead of if or wile instead of while.

    Why it fails

    Python will not recognize the misspelled form as the intended keyword.

    Do this instead

    Use the exact keyword spelling.

  • Common mistake

    Leaving out the colon at the end of an if or while line

    What it looks like

    Writing an if statement without its final colon.

    Why it fails

    The colon is part of the grammatical pattern for these statements.

    Do this instead

    Check that the if or while line ends with a colon.

  • Common mistake

    Leaving out parentheses in a function call

    What it looks like

    Calling a function without the required parentheses.

    Why it fails

    The source states that function calls must include parentheses.

    Do this instead

    Include the required parentheses when writing a function call.

  • Common mistake

    Using inconsistent variable-name spelling

    What it looks like

    Defining student_name and later using studentname or student_Name.

    Why it fails

    Python treats differently spelled names as different variables.

    Do this instead

    Copy the same spelling and capitalization whenever you reuse the name.

Best practice

When Python reports a syntax error, inspect the statement from left to right. First check the spelling and capitalization of its words. Then check the required order, punctuation, parentheses, and colon. This separates vocabulary problems from grammar problems and gives you a concrete place to begin correcting the statement.

Practice the Two-Part Check

EASY

For each statement you write, perform two checks: first, are all Python words and names spelled with the exact required characters? Second, are those words arranged according to the statement's grammatical pattern? Apply the check to an if statement, a function call, and a variable name that you reuse later.

Hints
  • For an if statement, look for the keyword, the condition, and the ending colon.
  • For a function call, check for parentheses.
  • For a reused variable name, compare capitalization and every underscore.

Key Takeaways

  • Python has vocabulary that must be spelled precisely and grammar that controls how words are arranged.
  • Python is case-sensitive, so differences such as Print versus print and Score versus score matter.
  • If statements require a condition and a colon, while function calls require parentheses.
  • Misspelled words, inconsistent names, missing punctuation, and incorrect arrangements can cause syntax errors.
  • Checking spelling first and grammatical structure second provides a practical way to diagnose malformed statements.

Related concepts

More concepts starting with D