Brief Overview of Python
Starting with Python
Write and run a first Python program.
A program is an ordered set of instructions for a computer, written in a programming language such as Python, C, C++ or Java. Python is a popular, easy-to-learn language created by Guido van Rossum in 1991, used in software and web development, scientific computing, big data and AI. It is open-source, high-level and interpreted.
Running Python. You need a Python interpreter (also called the Python shell), installed or online. The >>> symbol is the Python prompt: the interpreter is ready for instructions.
- Interactive mode: type a statement at >>> and press Enter; it runs at once. Good for testing one line, but the statements cannot be saved.
- Script mode: write the program in a file with the .py extension (a script), save it and run it. Python's built-in editor IDLE (Integrated Development and Learning Environment) can create it (File > New File) and run it (Run > Run Module); the output appears in the shell.
Keywords are reserved words with special meaning, such as False, None, True, and, or, not, if, elif, else, for, while, def, return, import, in, is, class, try. Python is case sensitive, so keywords must be written exactly (True, not true).
Identifiers name variables, functions and other things. The rules:
- begin with a letter (upper or lower case) or an underscore (_), followed by letters, digits or underscores;
- cannot start with a digit; can be of any length (short and meaningful is best);
- cannot be a keyword;
- no special symbols such as !, @, #, $, %.
- Choose meaningful names: marksMaths, marksEnglish, marksIP and avg tell you more than a, b, c.
Variables. A variable is an identifier whose value can change. It is created by an assignment statement, for example gender = 'M', message = "Keep Smiling", price = 987.9. A variable must be assigned a value before it is used, or Python reports an error.
Comments start with #; everything after # on that line is ignored. They explain the code to people.
Example (new): in a script saved as fees.py, the lines tuition = 1500 and transport = 600, then total = tuition + transport and print(total), would display 2100. Names like 2ndFee or total-fee would be rejected: one starts with a digit, the other contains a minus sign.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.