Master Python Basics: From First Program to Syntax Essentials
This tutorial walks you through Python fundamentals, covering interactive and script programming, identifiers, reserved keywords, indentation rules, multi‑line statements, string quoting, comments, blank lines, user input, multiple statements per line, code blocks, and command‑line arguments, with clear examples and code snippets.
First Python Program
Interactive Programming
Interactive programming does not require a script file; you write code directly in the Python interpreter.
On Linux, start it with the command python which shows a prompt like
>> Python 2.7.6 (default, Sep 9 2014, 15:04:36) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin. On Windows, the default interactive client is installed.
Script Programming
Save code in a .py file and run it with python test.py. Example:
print "Hello, Python!"Output: Hello, Python! Make the script executable on Unix:
#!/usr/bin/python
print "Hello, Python!"Then run:
chmod +x test.py
./test.pyOutput is the same as above.
Python Identifiers
Identifiers consist of letters, digits, and underscores, cannot start with a digit, and are case‑sensitive.
Names beginning with a single underscore (e.g., _foo) are intended for internal use; double underscores ( __foo) indicate private members; double underscores at both ends ( __foo__) denote special methods such as __init__.
Python Reserved Keywords
The following keywords cannot be used as identifiers:
and
exec
not
assert
finally
or
break
for
pass
class
from
continue
global
raise
def
if
return
del
import
try
elif
in
while
else
is
with
except
lambda
yield
Lines and Indentation
Python uses indentation instead of braces to define code blocks. All statements in the same block must have the same indentation level.
Incorrect indentation raises IndentationError. Consistent use of a single tab, two spaces, or four spaces is recommended.
Multi‑line Statements
Normally a newline ends a statement, but a backslash \ can continue a line. Parentheses, brackets, or braces also allow implicit line continuation.
Python String Quotes
Strings can be delimited by single ', double ", triple single ''', or triple double """ quotes. Triple quotes are useful for multi‑line strings and docstrings.
Python Comments
Single‑line comments start with #. Multi‑line comments can be written using triple quotes.
Blank Lines
Blank lines separate functions, classes, or logical sections but are not part of Python syntax. They improve readability and maintainability.
Waiting for User Input
Use raw_input("\n\nPress the enter key to exit.") to pause execution until the user presses Enter.
Multiple Statements on One Line
Separate statements with a semicolon ;. Example:
#!/usr/bin/python
import sys; x = 'runoob'; sys.stdout.write(x + '
')Code Blocks
Statements with the same indentation form a code block (suite). Compound statements such as if, while, def, and class follow this rule.
Command‑Line Arguments
Python scripts can accept arguments; use -h to display help information.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
