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.
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:
variable
, Variable
, and VARIABLE
are three different identifiers.!
, @
, #
, $
, %
, etc. within identifiers.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.
Explanation:
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.Python allows line continuation via the backslash (\
) when a single statement spans multiple lines. This can make code easier to read and maintain.
Explanation:
total
variable.print()
function outputs the value of total
, which is the sum of 1, 2, and 3.Python supports three types of quotes to denote string literals:
'
)"
)'''
or """
).This flexibility allows you to choose the best type for your needs and to use quotes within strings without escaping them.
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 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.
Explanation:
Python provides a built-in function input()
to capture user input. This function pauses program execution and waits for user input.
Explanation:
input()
displays its string argument as a prompt (here asking for the user's name) and waits for the user to enter something.input()
returns the entered text as a string, which is then stored in user_name
.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.
Explanation:
import sys
imports the sys
module, which is necessary for the next statement.x = 'foo'
assigns the string 'foo'
to the variable x
.sys.stdout.write(x + '\n')
uses sys.stdout.write
to print the value of x
followed by a newline character to the console.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.