Introduction to Python -...

16
Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 1 Introduction to Python Eng. Ibraheem Lubbad September 17, 2016

Transcript of Introduction to Python -...

Islamic University Of Gaza

Faculty of Engineering

Computer Engineering Department

Lab 1

Introduction to Python

Eng. Ibraheem Lubbad

September 17, 2016

Introduction:

Python is a high-level, object-oriented programming language created by Guido van Rossum.

Python is used for software development at companies and organizations such as Google, Yahoo, and NASA, it is designed to be highly readable. It has a very easy-to-use and simple syntax, making it the perfect language for someone trying to learn computer programming for the first time.

It’s is an interpreted language. Interpreter is a program that converts the high-level program we write into low-level program that the computer understands.

There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. For our lab, we will use Python 2.7

Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for several reasons:

The high-level data types allow you to express complex operations in a single statement.

Java

Python

Statement grouping is done by indentation instead of beginning and ending brackets {}.

Java Python

No variable or argument declarations are necessary. as first example

Writing a Python Program:

Python programs must be written with a particular structure. The syntax must be correct, or the interpreter will generate error messages and not execute the program.

Listing 1.1 (hello.py) is one of the simplest Python programs that does something:

Installing Python:

Installing Python is an easy task, after download file, we simply double-click this icon to open the Python.

Example 1: hello.py

print( "This is a simple Python program" )

Then follow steps to setup Python:

The following icon appears after type search window from now on we simply double-click this icon to open the Python IDE, it is a simple IDE.

The command window, shell, appears:

There two different modes of programming:

1. Interactive Mode Programming:

Invoking the interpreter without passing a script file as a parameter.

We can see the prompt >>>, where we can write and execute Python code in interactive mode. At the prompt, enter your first command.

Output

2. Script Mode Programming (batch mode):

Invoking the interpreter with a script parameter begins process of writing and executing the lines of Python code one by one until the script is finished. When the script is finished, the interpreter is no longer active.

This becomes impractical for larger programs, so an alternative exists. In batch mode, we first write all the lines in a separate file, and then run them all at once.

Let us write a simple Python program in a script. Python files have extension .py type the following source code in a hello.py file.

In the shell window, click File → New File

A new window appears, without the Python prompt >>>

Where we can enter one or more Python commands before executing them all at once.

When you’re ready, click Run → Run Module (or just press the function key F5). IDLE will first ask you to save the file, then it will execute it, with the results shown back in the shell window.

In our lab we will use another IDE (Integrated development environment) called PyCharm Community Edition:

Firstly sure Python interpreter is downloaded and installed on your computer (that we did it)

Then make double-click this icon to open the PyCharm then click Next >

Click on Create New Project:

Or From File----> New Project

Select location project and its name, and sure you select correct Python interpret

Then click on Create.

Select Python interpret

Right Click on the Project File in our example (FirstProject)

Then click New → Python File

Insert a new File Name

And Kind of file in our case (Python)

PyCharm WorkSpace:

After when you are write script code click Run → Run “script file” (or press the function key shift +F10).

Also you can run Python shell through Pycharm by click Tools → Python Console

Test Python Console:

Project Files

Script Window

Output Window

Work Lab:

Output of Example 2 (arrow.py):

Example 2: arrow.py

print(" * ")

print(" *** ")

print(" ***** ")

print(" * ")

print(" * ")

print(" * ")

print(" * ")

Exercises:

1) What is an interpreter?

2) What is necessary to execute a Python program?

3) Write a program that displays “Welcome to Engineering”, “Welcome to Python”,

and “Programming is fun” (Display three messages).

4) Write a program that displays the following pattern? (Display a pattern).

END