Python Syntax Errors and Basic Operations, CS 101 Unit 1 – Study Notes
offline

Difficulty: Beginner | Prerequisites: None, though basic familiarity with using a computer terminal or command line is helpful.

Big Picture

This unit is the very first step in learning Python. It covers how the Python interpreter reads your code, what happens when it finds something it cannot parse, and the handful of arithmetic and string operations you will use in every program you ever write. If you have never opened a Python shell before, this is where you start. The concepts here, syntax rules, data types, operators, variables, and the print() function, form the foundation that every later topic (conditionals, loops, functions) builds on, so getting comfortable with them early saves a lot of confusion later.


TL;DR

Python is strict about syntax: missing quotes, mismatched parentheses, or leading zeros on integers will all raise a SyntaxError before your code even runs. The basic arithmetic operators (* for multiplication, ** for exponentiation) and the + operator for joining strings together are the building blocks of almost every Python program. Wrapping a value in quotes makes it a string; leaving it bare makes it an integer, and the type() function tells you which one Python sees.


Key Terms

SyntaxError

An exception Python raises when it encounters code that violates the language's grammar rules, such as missing quotes, unmatched parentheses, or invalid numeric literals. Think of it as Python saying "I cannot even begin to run this, because the instruction itself is malformed."

String (str)

A sequence of characters enclosed in matching quotation marks (single '...' or double "..."). In simple terms, this means any text data: words, sentences, or even digits treated as text rather than numbers.

Integer (int)

A whole number with no decimal point, written without quotation marks. In simple terms, this means a plain number Python can do arithmetic with.

Operator

A symbol that tells Python to perform a specific computation on one or more values. * is multiplication, ** is exponentiation, + is addition (for numbers) or concatenation (for strings). Think of operators as the verbs in a Python expression.

Variable

A named label that stores a value in memory so you can refer to it later. You create one with the assignment operator =, for example city = "London". Think of it as a sticky note with a name on it, attached to a piece of data.

String concatenation

Joining two or more strings end-to-end using the + operator. "Hello, " + "World" produces "Hello, World". In simple terms, this means gluing text together.

type() function

A built-in Python function that returns the data type of whatever value you pass in. type('67') returns and type(67) returns . Think of it as asking Python "what kind of thing is this?"

print() function

A built-in function that outputs a value to the screen. It is the most common way to see results while learning Python.

Exponentiation

Raising a number to a power. In Python, this is done with **. So 8**8 means 8 raised to the power of 8, which equals 16,777,216. In simple terms, it is repeated multiplication: 8 multiplied by itself 8 times.

Octal literal

A number written in base-8 notation, prefixed with 0o in Python (for example, 0o17 is decimal 15). A leading zero on a decimal number such as 09 triggers a SyntaxError because Python reads that leading zero as an attempt at an octal literal, and 9 is not a valid octal digit.


Core Content: Syntax Errors in Python

Unterminated String Literals

  • A string must begin and end with matching quotation marks.

  • print(Hello, World') is missing the opening quote before Hello, so Python raises SyntaxError: unterminated string literal.

  • The fix: ensure every string has both an opening and a closing quote of the same type.

  • Example of correct usage: print('Hello, World')

Leading Zeros on Integers

  • Writing 09 in Python triggers SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers.

  • Python interprets a leading zero as the start of an octal (base-8) literal. Since the digit 9 does not exist in base 8, the expression is invalid.

  • The fix: drop the leading zero. print(9) works and outputs 9.

  • If you genuinely want an octal number, use the 0o prefix, for example 0o17 for decimal 15.

The Distinction Between * and **

  • * is the multiplication operator. 8 * 8 evaluates to 64.

  • ** is the exponentiation operator. 8 ** 8 evaluates to 16777216 (8 to the power of 8).

  • Typing * or ** on their own (with no operands) raises SyntaxError: invalid syntax because Python expects values on either side of an operator.

Strings vs. Integers and the type() Function

  • type('67') returns because the quotes tell Python to treat 67 as text.

  • type(67) returns because the bare number is treated as an integer.

  • The presence or absence of quotation marks is the only thing that determines whether Python sees text or a number. This distinction matters because arithmetic operations behave differently on strings and integers: '67' + '3' gives '673' (concatenation), while 67 + 3 gives 70 (addition).

Core Content: Basic Operations and Output

Arithmetic with Variables

  • You can store the result of a calculation in a variable and print it:

    • result = (35 * 2) stores 70 in the variable result.

    • print(result) outputs 70.

  • Parentheses here are optional for a single multiplication, but they make intent clearer and become essential when mixing operators.

String Variables and Concatenation

  • Assign text to variables just as you would numbers:

    • city = "Cleveland, "

    • country = "United States, "

    • continent = "North America"

  • Print them individually with separate print() calls, or join them with +:

    • location = city + country + continent

    • print(location) outputs Cleveland, United States, North America

  • The + operator, when used between strings, concatenates (joins) them. Spacing and punctuation must be included inside the strings themselves, as Python does not add spaces automatically.

Mixing Strings and Variables in print()

  • You can build readable output by concatenating string literals with string variables:

    • print("AY2024-T2 Undergraduate Final Exam period is " + start_date + " to " + end_date)

    • This outputs something like AY2024-T2 Undergraduate Final Exam period is 1-11-2024 to 1-14-2024

  • You can also concatenate two string literals directly:

    • print("The temperature in Cleveland is " + "64°F.")

  • Important: the + operator only works between values of the same type. Trying to concatenate a string and an integer (for example, "Age: " + 25) raises a TypeError. You would need "Age: " + str(25) to convert the integer to a string first.

Real-World Application

These operations are how every Python program builds its output. Web applications concatenate strings to produce HTML. Data scripts combine calculated results with labels to produce reports. The pattern of storing a value in a variable, transforming it, and printing a readable result is the core loop of practical programming.


Common Misconceptions

  • Students often think '67' and 67 are the same thing. They are not. The first is a string (text), the second is an integer (a number). You cannot do arithmetic with '67' unless you convert it first with int('67').

  • Students often think * and ** are interchangeable or that ** is just "double multiplication." ** is exponentiation: 2 ** 3 is 8, not 6.

  • Students often think a leading zero like 09 is fine because it means nine. Python disagrees: a leading zero signals an octal literal, and since 9 is not a valid octal digit, it raises a SyntaxError. Just write 9.

  • Students often think + always means addition. Between two strings, + joins them together (concatenation). Between a string and an integer, it raises a TypeError.


Why It Matters / Exam Flags

⚠️ Expect questions that show you a line of Python with a deliberate syntax error and ask you to identify what is wrong and how to fix it. Unterminated strings and mismatched parentheses are the most commonly tested.

⚠️ Know the difference between * (multiplication) and ** (exponentiation). A question might show 2 * 3 vs. 2 ** 3 and ask for the output of each.

⚠️ Be able to predict the output of type() for a given value. If a value is wrapped in quotes, it is a string regardless of whether it looks like a number.

⚠️ String concatenation with + is commonly tested. Know that you can only concatenate strings with strings, and that you must include your own spacing inside the string literals.


Quick Self-Test

  1. True or False: print('Hello World) will run without error. Answer: False. The closing quote is missing, so Python raises a SyntaxError.

  1. Fill in the blank: The operator ** performs _______ in Python. Answer: exponentiation (raising to a power).

  1. True or False: type('100') returns . Answer: False. The quotes make it a string, so it returns .

  1. Fill in the blank: To join two strings together with +, both values must be of type _______. Answer: str (string).

  1. True or False: print(07) is valid Python 3 syntax. Answer: False. Leading zeros on decimal integers are not permitted.


Practice Q&A

Q: What is the output of 8 ** 3 in Python?

A: 512. The ** operator raises 8 to the power of 3 (8 × 8 × 8).

Q: A student writes print(Hello, World'). What error does Python raise, and why?

A: Python raises SyntaxError: unterminated string literal. The opening quotation mark before Hello is missing, so Python does not know where the string begins.

Q: What is the difference between type('42') and type(42)?

A: type('42') returns because the quotation marks make it a string. type(42) returns because the bare number is an integer.

Q: Write a line of Python that stores your first name in a variable called name and prints it.

A: name = "Alice" followed by print(name). Any valid string works for the name value.

Q: What happens if you try to run print("Score: " + 95) in Python?

A: Python raises a TypeError because you cannot concatenate a string and an integer. The fix is to convert the integer: print("Score: " + str(95)).


Connections to Other Topics

This connects to control flow (Unit 2 and beyond) because every if statement and while loop depends on expressions built from these same operators and data types. Understanding why '5' > '40' evaluates to True (string comparison, not numeric) requires knowing how Python distinguishes strings from integers, which is exactly what the type() function demonstrates here.

String concatenation with + is the simplest form of building output, but later units introduce f-strings and .format(), which solve the problem of mixing strings and numbers more elegantly. Getting comfortable with + now makes those later tools easier to appreciate.


Related Terms / Search Tags

Python syntax error, SyntaxError, unterminated string literal, Python string, Python integer, str vs int, type() function, Python print function, string concatenation, Python operators, multiplication operator, exponentiation operator, Python variables, variable assignment, octal literal, leading zeros Python, Python data types, intro to Python, CS 101 Python, beginner Python, Python basics, arithmetic operators Python, print() Python 3