Before writing code it helps to know what Python is and why it is chosen. The language was designed so that programmers spend their time on the problem rather than on the syntax.

History of Python

Python was created by Guido van Rossum in the Netherlands and first released in 1991. The name comes from the comedy show Monty Python's Flying Circus and not from the snake. Python 2 was released in 2000 and Python 3 in 2008, and Python 3 is the version used today. Python is interpreted, which means that the code is run line by line by an interpreter, dynamically typed, which means that a variable need not be declared with a type, and open source, so it is free to use.

Definition: Python. A high-level, interpreted, general-purpose programming language known for its simple, readable syntax.

Application Areas

Python is used for web development with frameworks such as Django and Flask, for data science and machine learning with NumPy, pandas and scikit-learn, and for automation and scripting. It is also used in scientific computing, game development, network programming and teaching. Large organisations use it in production, and its large library of ready-made modules saves effort.

Structure of a Python Program

A Python program is a text file with the extension .py. It has no main function that must be declared and no semicolons at the end of lines. Instead, indentation, the blank space at the start of a line, marks a block of code. A typical program has comments, optional import statements, definitions of functions, and the statements that run. Comments begin with the symbol # and are ignored by the interpreter.

Python can be run in two ways. In interactive mode each statement is typed at the prompt and its result is shown at once, which is useful for trying small ideas. In script mode the statements are saved in a .py file and the whole file is run together. Beginners usually test an idea interactively and then move the working code into a script. Python also has a standard library of modules for mathematics, dates, files and networking, so a programmer can often import a ready-made tool instead of writing it again.

Table 1.1: Python compared with C

Feature C Python
Execution Compiled Interpreted
Typing Static, declared Dynamic, not declared
Blocks Braces Indentation
Line end Semicolon Newline

Example 1.1: Structure of a simple program

Problem: Write a program that stores the length and breadth of a room and prints its area.

Solution:

  1. Assign the two values to variables.
  2. Multiply them and print the result.

Result: The program below prints the area as 60.

# area of a room
length = 12
breadth = 5
area = length * breadth
print("Area =", area)

Output

Area = 60

Common mistakes

  • Mixing tabs and spaces for indentation, which causes an error.
  • Putting a semicolon or braces as in C.
  • Forgetting that Python is case sensitive, so Area and area are different names.

Key takeaways

  • Python was created by Guido van Rossum and released in 1991.
  • It is interpreted and dynamically typed.
  • Indentation marks blocks of code.
  • A comment begins with #.

Practice questions

  1. Who created Python?
  2. What marks a block of code in Python?
  3. Name two application areas of Python.

Answers to the practice questions

  1. Guido van Rossum.
  2. Indentation.
  3. Web development and data science.

Sources

  • Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit I