Learning Python By Example – Understanding the Basics

Learning Python By Example, Understanding the Basics First. Python is a very simple language which is widely used, and has a very straightforward syntax. It motivates programmers to program without boilerplate (prepared) code. The simplest directive in Python is the “print” directive – it simply prints out a line (and also includes a newline, unlike in C).

Please remember to go through our previous tutorial on how to get started

There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 3, because it more semantically correct and supports newer features.

For example, one difference between Python 2 and 3 is the print statement. In Python 2, the “print” statement is not a function, and therefore it is invoked without parentheses. However, in Python 3, it is a function, and must be invoked with parentheses.

Many programmers learn through experimentation, looking at others’ code and working out what method is best for a given situation. This tutorial is a hands-on approach to learning programming. After minimal reading you are set a number of challenges to create the programs. You can explore and experiment with the programming language and look at the example solutions to learn how to think like a programmer.

Running Your Program

Every time you run the code your program will need to be saved afresh in case there have been any changes to it.

In this version of Python, you can run the program by selecting the Run menu and selecting Run Module. Alternatively, you can press the [F5] key. If this is the first time the program is saved, Python will prompt you to name and save the file before it will allow the program to run.

File Location
On a Windows system, the Python folder is usually found in the C:\ drive and will be named Python39 (or similar) and the files will automatically

Important Things to Note When Writing Your Programs

  • Python is case sensitive so it is important that you use the correct case, otherwise your code will not work.
  • Text values need to appear in speech marks (“) but numbers do
    not.
  • When naming variables (i.e. values that you want to store data in) you cannot use any dedicated words such as print, input, etc. otherwise your code will not work.
  • Variable names can’t contain spaces
  • When saving your files do not save them with any dedicated words that Python already uses, such as print, input, etc. If you do this it will not run and you will need to rename the file before it works.
  • To edit a program you have saved and closed, right-click on the
    file and select Edit with IDLE or Pycharm. If you just double-click on the file it will only try to run it and you will not be able to edit it.
  • Python uses indentation for blocks, instead of curly braces. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces.

Using Comments

Comments are a very useful tool for programmers. They serve two purposes:

  • adding an explanation of how the program works;
  • stopping parts of the program from working temporarily so you can run and test other parts of the program.

The first, and original, purpose of explaining how a program works is so other programmers can make sense of your programs in case your code needs to be altered and updated in the future and to remind you about why you wrote particular lines of code.

python comment1

 

In the above example, comments have been added at the end of the last three lines. They are shown in red and start with the # symbol.
In reality, you would not add comments on lines which contain obvious code as it would clutter the screen; you would only add comments where necessary.
As Python knows to ignore anything after a # symbol, programmers soon started to use # at the start of lines of their code to block out sections they do not want to run so they can focus on and test others.

python comment2

In this example, the # has been added to the first line of the program to temporarily stop it from running. To bring it back into the running order simply delete the # and the code will be reactivated.

Examples of Code - Explanation

number1 = 100
Set the value of a variable, if there is not a variable already created, it will create one. A variable is a container for a value (in this case the variable will be called “number1” and store the value 100). The value stored in the variable can change while the program is running. The variable can be called whatever you want (except Python dedicated words such as print, save, etc.) and it must start with a letter rather than a number or symbol and have no spaces.

answer = num1 + num2
Adds together num1 and num2 and stores the answer in a variable called answer.

answer = num1 – num2
Subtracts num2 from num1 and stores the answer in a variable called answer.

answer = num1 * num2
Multiplies num1 by num2 and stores the answer in a variable called answer.

answer = num1 / num2
Divides num1 by num2 and stores the answer in a variable called answer.

answer = num1 // num2
A whole number division (i.e. 9//4 = 2) and stores the answer in a variable called answer.

print (“This is a message”)
Displays the message in the brackets. As the value we want displayed is a text value it has the speech marks, which will not be displayed in the output. If you wanted to display a numerical value or the contents of a variable, the speech marks are not needed.

print (“First line\nSecond line”)
“\n” is used as a line break.

textValue = input(“Enter a text value: ”)
Displays the question “Enter a text value: ” and stores the value the user enters in a variable called textValue. The space after the colon allows a space to be added before the user enters their answer, otherwise they appear squashed unattractively together.

print (“The answer is”, answer)
Displays the text “The answer is” and the value of the variable answer.

numValue = int(input(“Enter a number: ”))
Displays the question “Enter a number: ” and stores the value as an
integer (a whole number) in a variable called numValue Integers can be used in calculations but variables stored as text cannot.

Leave a Comment