Course Four - The basic structure of the Python language

The Basic Structure of Python

By ienex



Python is a versatile programming language with similarities to Perl, C, and Java, but it has its own unique features and syntax that make it simpler and more readable.


Writing Your First Python Program

Python allows you to write programs in two primary ways: interactive programming and scripting.

1. Interactive Programming

In interactive mode, you can start the Python interpreter without passing a script file:

$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Type the following command:

>>> print("Hello, Python!")

The output will be:

Hello, Python!

Note: In older versions of Python (like 2.x), print statements may not require parentheses.


2. Scripting

You can also write Python programs as script files with a .py extension. Example:

# test.py
print("Hello, Python!")

Run the script from the terminal:

$ python test.py
Hello, Python!

You can also make the script executable on Unix/Linux systems:

$ chmod +x test.py
$ ./test.py
Hello, Python!

Python Identifiers

A Python identifier is a name used for variables, functions, classes, or other objects.

  • Must start with a letter (A-Z, a-z) or underscore (_)

  • Can include letters, numbers (0-9), or underscores after the first character

  • Case-sensitive (myVar and myvar are different)

  • Cannot include special symbols such as @, $, %

Naming conventions:

  • Class names start with a capital letter.

  • Variables and functions start with a lowercase letter.

  • Single leading underscore (_var) indicates a “protected” identifier.

  • Double leading underscore (__var) indicates a special or private identifier.

  • Trailing underscore (var_) avoids conflict with reserved keywords.


Reserved Keywords

Python has reserved words that cannot be used as identifiers:

and, as, assert, break, class, continue, def, del, elif, else, except, 
finally, for, from, global, if, import, in, is, lambda, nonlocal, not, 
or, pass, raise, return, try, while, with, yield

Indentation and Blocks

Unlike C or Java, Python does not use braces {} to define code blocks. Instead, indentation determines blocks.

Example:

if True:
    print("True")
else:
    print("False")

All lines within the same block must have the same number of spaces. Inconsistent indentation will cause errors.


Multi-Line Statements

Use the backslash \ to split a statement across multiple lines:

total = item_one + \
        item_two + \
        item_three

Data structures like lists, dictionaries, or tuples do not require a backslash:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

Strings and Quotes

Python supports multiple ways to define strings:

word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph
that spans multiple lines."""

Triple quotes """ """ are used for multi-line strings.


Comments

Comments begin with # and extend to the end of the line. They are ignored by the interpreter:

# This is a comment
print("Hello, Python!")  # Inline comment

Output:

Hello, Python!

Python’s simple syntax, clear structure, and versatile string handling make it easy for beginners to start programming while offering advanced features for more experienced developers.

Comments

Post a Comment

Popular posts from this blog

Why You Should Learn JavaScript and Python in 2025

Why you Should Learn JavaScript and Python ?

Course Six - Operators in Python