Concepts / Getting Started: Why Programming Matters and How Computers Work

Getting Started: Why Programming Matters and How Computers Work

Programming languages, like natural languages, have vocabulary (words that must be spelled correctly) and grammar (rules for arranging those words into valid statements).

  • Programming

Programming as Communication

Programming is a way to communicate instructions to a computer. Python is a programming language, so it works in a similar way to a natural language: it has vocabulary, consisting of words and symbols that Python recognizes, and grammar, consisting of rules for arranging those elements. Learning both is the first essential programming skill because Python can execute only instructions that are written in a form it understands.

From Words to Statements

Think of Python code as being built in two stages. First, you use words and other elements from Python's vocabulary. Then, you arrange them according to grammar rules. Knowing the word if is not enough to create an if statement. The word must be followed by a condition, and the statement must end with a colon. The vocabulary supplies the pieces; the grammar determines how those pieces fit together.

followed byfollowed byifkeywordconditiontest to evaluate:required ending
How do Python words fit together in the correct order to form a complete statement?

A well-formed statement needs both correct vocabulary and correct arrangement. Correct words in the wrong order are not enough, and correct ordering cannot repair a misspelled Python word.

Spelling and Capitalization

Python is case-sensitive. This means that uppercase and lowercase letters are significant: Print is not the same as print, and Score is not the same as score. Every character must match exactly. Python also has fixed keywords, including if, for, while, def, and return, and those keywords must be spelled precisely.

Checking a Python name

A programmer creates a variable named student_name and later writes studentname or student_Name. What should the programmer notice?

Compare the characters: The later names do not match student_name exactly. One changes the underscore, and the other changes the capitalization of the letter N.

Apply Python's rule: Python treats differently spelled or differently capitalized names as different variables. A name must be used consistently every time.

Diagnose the result: The mismatch can lead to unexpected behavior or errors because Python does not treat the names as identical.

Use the exact same spelling and capitalization for a name each time it appears.

Written formPython's interpretation
printDifferent from Print
scoreDifferent from Score
student_nameMust be used with the same spelling and capitalization
studentnameA different name from student_name

Grammar Patterns

Python has grammatical rules for each type of statement. An if statement uses the keyword if, followed by a condition, followed by a colon. A function definition starts with def, followed by the function name, followed by parentheses and a colon. Function calls must include parentheses. These patterns tell Python how to parse and execute the instruction.

Statement typeRequired pattern
if statementif, then a condition, then a colon
function definitiondef, then a function name, then parentheses, then a colon
function callThe function call includes parentheses

Selected Python grammar patterns described in the source material.

The punctuation in these patterns is part of the grammar, not decoration. Leaving out the colon at the end of an if statement or while statement violates the required pattern. Leaving out a closing parenthesis also makes the statement incomplete. In such cases, Python reports a syntax error and refuses to run the malformed code.

Valid and Invalid Syntax

if condition :complete patternif conditioncolon missing
What specifically changes between a correctly written Python statement and one that produces a syntax error?

Finding the defect

Compare two attempted if statements: one has the keyword if, a condition, and a colon; the other has the keyword if and a condition but no colon.

Check the vocabulary: Both attempts use the Python keyword if.

Check the order: Both attempts place the condition after if.

Check the required ending: Only the first attempt includes the colon required at the end of an if statement.

Classify the statements: The first follows the stated pattern. The second violates Python grammar and causes a syntax error.

A statement can contain recognizable Python words and still be invalid when required punctuation is missing.

Mistakes Beginners Make

  • Changing capitalization in a Python keyword or name

    Python is case-sensitive, so the changed spelling refers to something different.

    Fix: Match every character, including uppercase and lowercase letters.

  • Misspelling a fixed keyword

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

    Fix: Use the exact spelling of the keyword.

  • Using a created name inconsistently

    Python treats differently spelled names as different variables, which can cause unexpected behavior or errors.

    Fix: Repeat the same spelling and capitalization every time.

  • Leaving out required punctuation

    The statement no longer follows the grammatical pattern required by Python and can produce a syntax error.

    Fix: Check the required punctuation for the statement type.

When Python rejects a statement, inspect two things separately: first, whether every word and name is spelled exactly as intended; second, whether the elements appear in the required order and include required punctuation. This separates vocabulary errors from grammar errors.

Practice the Check

EASY

For each situation, decide whether the main problem is spelling and capitalization or grammar and punctuation: writing Print instead of print; writing an if statement without its colon; using studentname after defining student_name; writing a function call without parentheses.

Hints
  • Ask whether the characters match exactly.
  • Then ask whether the statement includes the required parts and punctuation.
  1. The first two checks for any Python statement are vocabulary and grammar. Vocabulary asks whether the words, names, and capitalization are correct. Grammar asks whether those elements are arranged according to the statement's pattern and whether required punctuation is present.

The Foundation for Programming

Python's vocabulary and grammar are finite and learnable. Once you can spell Python words correctly, preserve capitalization, use names consistently, and arrange statement parts according to their grammatical rules, you can write instructions Python can understand and execute. This foundation comes before the next essential programming skill: problem-solving and logical thinking.

  • Python has vocabulary: keywords, names, operators, and literals that must be written as Python expects.
  • Python is case-sensitive, so capitalization changes meaning or identity.
  • Python has grammar: rules for arranging vocabulary into valid statements.
  • Missing punctuation, incorrect order, or malformed statements can cause syntax errors.
  • Mastering vocabulary and grammar is the first essential skill of programming.

Key Takeaways

  • Programming languages use vocabulary and grammar just as natural languages do.
  • Python is case-sensitive and requires exact spelling for keywords and names.
  • Different statement types have specific grammatical patterns, including required colons and parentheses.
  • A syntax error occurs when code violates Python's grammatical rules, while inconsistent spelling can make Python treat names as different.
  • Vocabulary and grammar provide the foundation for later problem-solving and logical thinking.