PYTHON SYNTAX A Complete Beginner - Friendly Guide Prepared by Rachit Joshi Based on the Python Syntax tutorial from TpointTech Python Syntax Python syntax is the set of rules that determines how Python code is written and organized. Python is known for its readable structure, which makes it approachable for beginners while remaining useful for experienced developers. This guide explains the main syntax rules, with clear examples and expected outputs. 1. What Is Python Syntax? Python syntax is similar to the grammar of a human language. It tells the Python interpreter how to understand statements and execute instructions. Correct syntax helps programs run as intended and makes code easier to read, debug, and maintain. print("Hello, World!") Output: Hello, World! 2. Interactive Mode and Script Mode Interactive Mode Interactive mode lets you type commands directly into the Python interpreter and see the result immediately. Start Python from a terminal; the >>> prompt indicates that Python is ready for a command. >>> print("Hello, World!") Hello, World! Script Mode In script mode, save Python statements in a file with the .py extension, then run the file from a terminal. # Save this as sample.py print("Hello, World!") # Run it from a terminal: python sample.py Output: Hello, World! 3. Variables Variables store values. Python is dynamically typed, so you do not need to declare a variable's type before assigning a value. The type is determined from the value assigned. age = 20 course = "Python" print(age, type(age)) print(course, type(course)) Output: 20 <class 'int'> Python <class 'str'> Use meaningful names and assign values with the equals sign (=). 4. Indentation Indentation is essential in Python. It marks blocks of code, such as the bodies of if statements, loops, and functions. Use consistent indentation — commonly four spaces per level. if 9 > 5: print("9 is greater than 5") print("This line is inside the if block") print("This line is outside the if block") Output: 9 is greater than 5 This line is inside the if block This line is outside the if block Incorrect or inconsistent indentation can cause an IndentationError or change how a program behaves. 5. Identifiers and Naming Rules Identifiers are names used for variables, functions, classes, and other objects. Follow these rules: • An identifier may contain letters (A – Z, a – z), digits (0 – 9), and underscores (_). • It must not begin with a digit. • It must not be a reserved Python keyword. • Identifiers are case - sensitive: total, Total, and TOTAL are different names. • Prefer descriptive names. Use snake_case for variables and functions, and CapWords (often called CamelCase) for class names. Examples student_name = "Asha" # valid score2 = 95 # valid _total = 120 # valid # Invalid examples: # 2score = 95 # class = "Python" The invalid examples are commented out so the sample can be read without causing a syntax error. 6. Python Keywords Keywords are reserved words with special meaning in Python. They cannot be used as ordinary identifiers. Common examples include if, else, for, while, def, class, return, import, try, and except. The keyword set can vary between Python versions; use the keyword module to check the active interpreter. import keyword print(keyword.kwlist) This prints the list of keywords supported by the Python version running the program. 7. Comments Comments explain code to people reading it. Python ignores text after # on a line when it is outside a string. Use comments to clarify intent rather than repeat what the code already says. # Calculate the total price price = 25 tax = 3 total = price + tax # Add price and tax print("Total:", total) Output: Total: 28 Triple - quoted strings are commonly used as docstrings when placed at the beginning of a module, class, or function. They are string literals, not a dedicated multiline - comment syntax. def greet(): """Return a friendly greeting.""" return "Hello!" 8. Multiline Statements Long statements can be split across lines to improve readability. Parentheses are usually the clearest option. A backslash can also explicitly continue a line, but should be used sparingly. Using parentheses (recommended) total_cost = ( 25 + 58 + 92 + 74 + 29 + 82 + 51 + 99 + 12 ) print(total_cost) Output: 522 Using a backslash total_cost = 25 + 58 + 92 + \ 74 + 29 + 82 + \ 51 + 99 + 12 print(total_cost) 9. Taking Input from the User The input() function reads text entered by the user and returns it as a string. Convert the result when a numeric value is required. first_name = input("Enter your first name: ") last_name = input("Enter your last name: ") print("Welcome,", first_name, last_name) Example interaction: Enter your first name: Tony Enter your last name: Prince Welcome, Tony Prince Reading a number age = int(input("Enter your age: ")) print("Next year you will be", age + 1) Use int() for whole numbers or float() for decimal numbers. If the entered text cannot be converted, Python raises a ValueError. 10. Other Useful Syntax Basics Case sensitivity language = "Python" Language = "Java" print(language) print(Language) These are two separate variables because Python is case - sensitive. Colons and code blocks A colon (:) introduces a suite or block in constructs such as if, for, while, def, and class. The following block is indented. for number in range(3): print(number) Output: 0 1 2 Line breaks and whitespace Spaces around operators are not always required, but they improve readability. Follow consistent formatting conventions, such as those described in PEP 8. 11. Common Syntax Errors • Missing colon: write if condition: rather than if condition. • Incorrect indentation: align statements belonging to the same block. • Unmatched quotes or brackets: ensure strings and parentheses are properly closed. • Using a keyword as a name: choose a different identifier, such as class_name instead of class. • Forgetting input conversion: input() returns a string, so convert it before arithmetic when needed. • Incorrect capitalization: Python treats Print and print as different names; the built - in function is print(). 12. Practice Examples Try reading each example, predicting the output, and then running it in Python. Example 1: Simple calculation a = 12 b = 8 print(a + b) print(a * b) Output: 20 96 Example 2: Conditional statement temperature = 30 if temperature > 25: print("It is warm.") else: print("It is cool.") Output: It is warm. Example 3: Function def add_numbers(a, b): return a + b print(add_numbers(4, 6)) Output: 10 Conclusion Python syntax provides a consistent, readable way to express instructions. By understanding indentation, variables, identifiers, keywords, comments, multiline statements, and user input, beginners can start writing reliable Python programs. Regular practice is the best way to become comfortable with these rules.