WEB Programmer - Python

Unit 1

Unit 1: The Python programming language

AIMS & OBJECTIVES

1) The purpose of this Unit is to enhance migrants’ basic soft skills and competencies, which are needed in today’s labor market and help them in their fist steps while in search for a job.

2) The Unit includes four units, focusing on themes such as installing Python, working with variables, lists, while loop, arithmetic operations, generators in Python and other.

3) The main part of this presentation is the compilation of programmers with Python

4) The Unit aims at helping the reader to understand «Installing Python», «the interpreter», «create your first program» and «working with variables».

LEARNING OUTCOMES

After completing the course you will attain a fundamental understanding of the Python programming language, object oriented programming base, essential skills to specialize on particular branches — machine learning, data science etc.

KEYWORDS

Python
•Most popular programming languages (human readable)
Guido van Rossum
•Machine learning
•Working with variables
•Evaluative logic
•Branching with a conditional operator
•Development of interfaces

TABLE OF CONTENTS

1.1 Introduction

Python is a high-level (“human readable”) programming language that uses an interpreter to display results. Python contains an extensive standard library of tested code modules that can be easily incorporated into your own programs.

Developed by GuidovanRossum in the late 1980s and early 1990s at the National Research Institute of Mathematics and Computer Science in the Netherlands, Python is derived from many other languages, including C, C ++ and the Unix shell. Today Python is supported by the core team at the institute, although Guido vanRossum still plays an important role in determining the direction of the language. Code readability, making Python especially suitable for newbies to programming.

Because Python is focused on code readability, it often uses English keywords where other programming languages tend to use punctuation marks. A special difference is that Python uses indentation rather than keywords or punctuation marks to group statements in a block of code.

1.2. Installing Python on Windows Operating System

  1. Now confirm the suggested installation location, which will include the root drive name, the word Python and the version number – in this example, the installation will be in the C: \ Python33 directory for version 3.3.2.
  1. Click Next to continue and make sure the Add python.exe toPath component is selected.

1.2. Installing Python on Windows Operating System

  1. Click Next to begin copying the files to your computer, and then Finish to complete the installation process.
  2. 6. To verify that Python is now available, restart your computer, run a command prompt (cmd.exe), and type python –V, and the Python interpreter will return the installed version number.

1.3 Getting to know the interpreter

The Python interpreter processes your program’s textual code and also has an interactive mode, useful for debugging and testing snippets of code. Python interactive mode can be accessed in several ways:

  • from a normal command line – enter the python command to run the initial Python command line (symbols >>>), in which you interact with the interpreter;

1.3 Getting to know the interpreter

  • from the Start menu – select the Python commandline – a window will open containing the initial command line of the Python interpreter with the symbols >>>;
  • from the Start menu – select IDLE (Python GUI) to launch a Python shell window containing a command line with >>> characters.

1.3 Getting to know the interpreter

Regardless of which method you choose to enter interactive mode, the Python interpreter will respond in the same way to commands entered on its command line after the >>> characters. In this mode, it can be used as a calculator.

  1. 1. Enter Python interactive mode using any of the above methods, then type a simple expression with addition and press the Enter key. The interpreter will give you the amount in response.

1.3 Getting to know the interpreter

The Python interpreter understands any arithmetic expression, so you can use parentheses to indicate the order of evaluation – the part of the expression enclosed in parentheses will be evaluated first.

  1. Then on the Python command line, type an expression with three operands without specifying the order of evaluation.

3. Now on the Python command line, type the same expression, but add parentheses to indicate the order of evaluation.

1.4 Your first program

In addition to being useful as a basic calculator, Python interactive mode can be used to create programs. A Python program is a plain text file created with a simple editor such as Notepad and saved as a .py file. You can run a Python program by specifying the name of the corresponding file after the python command in the interpreter command line. By tradition, the first program that is created when learning a programming language simply displays a message with a greeting. In Python, the print () function is used for this, the message for the output of this function is indicated in brackets. It can be a character string enclosed in quotation marks. Quotes can be double (“”) or single (”), but you cannot use both at the same time.

1.4 Your first program

  1. On a Windows computer, start a simple text editor such as Notepad.
  2. Then type the following statement on a blank line in the editor: print (‘HelloWorld!’)
  3. Now create a new directory C: \ MyScripts and save the file in it as hello.py

1.4 Your first program

  1. Now open a command prompt window, navigate to the directory you just created and type python hello.py – you will see the Python interpreter run your program and display a welcome message.

1.4 Your first program

The procedure for creating your first Python program on Linux is no different from that on Windows. However, regardless of the platform you are using, you should always remember that if different versions of Python are installed, then the correct command should be used to invoke the interpreter. This is especially important for Linux systems, which usually come with the installed version of Python 2.7, and the typed python command invokes this interpreter by default. If Python 3.3 is installed and you want to call it to process your program, then you should use the python3.3 command to invoke the correct version of the interpreter.

1.4 Your first program

  1. On Linux, start any text editor such as Nano.
  2. Then type the following statement in an empty editor window: print (‘HelloWorld!’)
  3. Now save the file in your home directory as hello.py.
  1. Finally, launch a terminal window and change to your home directory, and then type python3.3 hello.py – you will see the Python interpreter run your program and display the appropriate message.

1.5. Working with variables

In programming, a variable is a container in the computer’s memory where data is stored. After the data has been saved, it can be called using the name of this variable. The programmer can choose any name for the variable, except for Python keywords. It is best to choose meaningful names for variables that reflect their content.

In Python programs, data that needs to be stored in variables is entered using the assignment operator =, for example, to store the numeric value 8 in a variable named a, you would write: a = 8

You can then refer to the stored value of the variable using its name. Thus, the print (a) statement will print out the stored value 8. Variables can be sequentially assigned different values, and therefore a variable can take on different values as the program runs – it is no coincidence that it is called that: variable.

In Python, a variable must be assigned an initial value (variable initialization) in the statement that declares the variable in the program, otherwise the interpreter will raise the error notdefined (undefined variable).

1.5. Working with variables

In one statement, it is allowed to initialize several variables with the same value. This can be done using the assignment operator =. For example, to initialize variables a, b and c and assign them the value 8, we use the notation: a = b = c = 8

On the contrary, several variables can be initialized with different values and written all in one statement, using a comma as a separator. For example, as an initialization of variables a, b and c with numeric values 1, 2, 3 we use the notation: a, b, c = 1, 2, 3

Some programming languages, such as Java, require variable types to be specified when declaring them. In this case, a certain amount of memory is reserved. This technique is known as static typing. This restriction is not imposed on variables in Python, and memory is allocated in accordance with the values assigned to the variables (dynamic typing). This means that a variable can contain both integers and floating point numbers, text strings, or boolean values. You can add comments to your Python programs to describe instructions or sections of code. The # symbol is used for this. Anything after this character to the end of the line is ignored by the Python interpreter. Comments are very useful – they help make your code understandable for others, as well as for yourself when you come back to it later.

1.5. Working with variables

  1. Start a text editor, in which you declare and initialize a variable, then print the value stored in it.

# Initialize the variable with an integer value

var = 8

print (var)

  1. Then assign a new value to the variable and display it.

# Assign a floating point value to a variable

var = 3.142

print (var)

  1. Now assign a different value and display it again.

# Assign a string value to the variable

var = ‘Pythonineasysteps

print (var)

  1. Finally, we assign one more value and again display the result of the ultat.

# Assign a boolean value to the variable

var = True

print (var)

  1. Save the file in your working directory, then open a command prompt from that directory and run the program to see the output.

1.6. Retrieving user-entered data

Variables in Python can be assigned values not only through a program, but also through user input. The input () function is used for this. It takes a string as an argument, which will be displayed to the user, prompting him to enter data, and then reads the string entered by the user.

Such characters are interpreted as a text string, even if they are actually numeric values. This string can be assigned to any variable using the assignment operator =. Subsequently, you can work with this variable in the same way as with others, for example, display its value by specifying the variable name in the print () function.

Using the print () function, you can also print several values of variables by specifying them inside parentheses, separated by commas.

1.6. Retrieving user-entered data

  1. 1. Start a text editor in which you declare and initialize a variable by prompting for user input.

# Initialize the variable with the value entered by the user

user = input (‘I amPython. Whatisyourname?:’)

  1. 2. Then display a response message confirming the user-entered value.

# Print the string and the value of the variable

print (‘Welcome’, user)

  1. 3. Save the file in your working directory, open a command prompt and run the program – enter your name, press the Enter key, and you will see a response message containing your name.

When you print multiple values using the print () function, they are separated by a single space by default. To specify an alternate separator, you can add the sep parameter to the print () function. For example, if you use sep = ‘*’, you will get values in the output, separated by *.

Also, by default, the print () function prints a non-displayable newline character (\ n) at the end of each line. But it is possible to specify your own symbol using the end parameter. For

example, using end = ‘!’ will print an exclamation mark at the end of each line.

1.6. Retrieving user-entered data

  1. 4. Edit your program by declaring and initializing the second variable with another

user input prompt

# Initialize another variable with the value entered by the user

lang = input (‘Favorite programming language?:’)

  1. Print a response message to confirm user input by specifying your output separator and line terminator.

# Print the string and the value of the variable

print (lang, ‘Is’, ‘Fun’, sep = ‘*’, end = ‘! \ n’)

  1. Now save the file, open the command line and run the program again – enter your name and the name of the programming language. Then press the Enter key and you will see a response message containing your input.

SYNOPSIS

Python is a high-level (“human readable”) programming language that uses an interpreter to display results. Python contains an extensive standard library of tested code modules that can be easily incorporated into your own programs.

Developed by GuidovanRossum in the late 1980s and early 1990s at the National Research Institute of Mathematics and Computer Science in the Netherlands, Python is derived from many other languages, including C, C ++ and the Unix shell. Today Python is supported by the core team at the institute, although Guido vanRossum still plays an important role in determining the direction of the language. Code readability, making Python especially suitable for newbies to programming.

Because Python is focused on code readability, it often uses English keywords where other programming languages tend to use punctuation marks. A special difference is that Python uses indentation rather than keywords or punctuation marks to group statements in a block of code.

List of references

  • Eric Matthes, Python Crash Course. (No Starch Press, 2016)
  • Al Sweigart, Invent Your Own Computer Games with Python, 4th edition. (No Starch, 2017)
  • Dan Bader, Python Tricks: A Buffet of Awesome Python Features. (dbader.org, 2017)

Further reading

  • John Zelle, “Python Programming: An Introduction to Computer Science Paperback”. Franklin, Beedle&Associates Inc. www.fbeedle.com 3/09/10
  • Paul Barry, “Head First Python 2e: A Brain-Friendly Guide Paperback”. www.oreilly.com 16/12/16