0% completed
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
, andVARIABLE
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
Explanation:
- The indentation level (spaces or tabs) of lines within the same block must be the same.
- The
if
statement checks ifTrue
is true (which it always is), and the indented code that follows is executed as part of theif
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
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 oftotal
, 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
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
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
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 inuser_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
Explanation:
- This line includes three separate statements.
- The first statement
import sys
imports thesys
module, which is necessary for the next statement. - The second statement
x = 'foo'
assigns the string'foo'
to the variablex
. - The third statement
sys.stdout.write(x + '\n')
usessys.stdout.write
to print the value ofx
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.
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