Grokking Python Fundamentals
Ask Author
Back to course home

0% completed

Python - Basic Syntax
Table of Contents

Python Identifiers

Lines and Indentation

Example

Multi-Line Statements

Example

Quotation in Python

Example

Blank Lines

Example

User Input

Example

Multiple Statements on a Single Line

Example

Python is known for its readable and straightforward syntax that emphasizes clarity, which makes it an excellent choice for beginners in programming. Here, we will cover the fundamental aspects of Python's syntax, including its layout, conventions, and basic constructs.

Python Identifiers

An identifier is a name used to identify a variable, function, class, module, or other object. Python identifiers must start with a letter (A-Z or a-z) or an underscore (_) followed by zero or more letters, underscores, and digits (0-9).

Rules:

  • Python is case-sensitive: variable, Variable, and VARIABLE are three different identifiers.
  • Identifiers can be of any length.
  • You cannot use special symbols like !, @, #, $, %, etc. within identifiers.

Lines and Indentation

Python uses indentation to define blocks of code. Unlike many other languages that use braces {} to define blocks, Python uses indentation levels, which increases the code's readability.

Example

Python3
Python3

. . . .

Explanation:

  • The indentation level (spaces or tabs) of lines within the same block must be the same.
  • The if statement checks if True is true (which it always is), and the indented code that follows is executed as part of the if block.

Multi-Line Statements

Python allows line continuation via the backslash (\) when a single statement spans multiple lines. This can make code easier to read and maintain.

Example

Python3
Python3

. . . .

Explanation:

  • The statement starts with an assignment to total variable.
  • The backslash at the end of the first line indicates that the statement continues on the next line, allowing the statement to span three lines.
  • Each line after the backslash is a continuation of the statement from the previous line.
  • The print() function outputs the value of total, which is the sum of 1, 2, and 3.

Quotation in Python

Python supports three types of quotes to denote string literals:

  • single quotes (')
  • double quotes (")
  • triple quotes (''' or """).

This flexibility allows you to choose the best type for your needs and to use quotes within strings without escaping them.

Example

Python3
Python3

. . . .

Explanation:

  • word uses single quotes to enclose a simple string.
  • sentence uses double quotes, which is useful if the string itself contains a single quote (e.g., "It's sunny").
  • paragraph uses triple quotes, allowing the string to extend over multiple lines without using explicit line continuation, making it ideal for longer text or comments.

Blank Lines

Blank lines are not processed by Python but can be used to separate blocks of code visually. They can make your code more readable by breaking it into logical sections.

Example

Python3
Python3

. . . .

Explanation:

  • Blank lines are used here to separate the sections calculating the sum and the product, improving readability.
  • Python ignores these blank lines during execution, but they help developers to quickly identify related code blocks.

User Input

Python provides a built-in function input() to capture user input. This function pauses program execution and waits for user input.

Example

Python3
Python3
. . . .

Explanation:

  • input() displays its string argument as a prompt (here asking for the user's name) and waits for the user to enter something.
  • Once the user presses Enter, input() returns the entered text as a string, which is then stored in user_name.
  • The program then concatenates "Hello, " with the user's name and a exclamation point, and prints the greeting.

Multiple Statements on a Single Line

The semicolon (;) allows you to write multiple statements on a single line. This is typically used to condense simple statements into a single line for brevity, though it's generally discouraged as it can reduce readability.

Example

Python3
Python3

. . . .

Explanation:

  • This line includes three separate statements.
  • The first statement import sys imports the sys module, which is necessary for the next statement.
  • The second statement x = 'foo' assigns the string 'foo' to the variable x.
  • The third statement sys.stdout.write(x + '\n') uses sys.stdout.write to print the value of x followed by a newline character to the console.
  • Using a semicolon between each allows them to be executed sequentially on a single line.

This covers the basic syntax elements of Python, providing a foundation for writing well-structured and readable Python code. Understanding these basics is crucial as you start writing more complex Python programs.

Mark as Completed

Table of Contents

Python Identifiers

Lines and Indentation

Example

Multi-Line Statements

Example

Quotation in Python

Example

Blank Lines

Example

User Input

Example

Multiple Statements on a Single Line

Example