Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1...

15
Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Introduction to Computers Lab (ENGG 1003) ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ= /; Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji

Transcript of Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1...

Page 1: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

Islamic University of Gaza

Faculty of Engineering

Computer Engineering Dept.

Introduction to Computers Lab (ENGG 1003)

ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ

= /;

Lab1

Introduction to Python

Lab 1: Introduction to python

Eng. Mai Z. Alyazji

Page 2: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

Introduction

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

van Rossum.

- It 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.

- 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.

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

No variable or argument declarations are necessary, first example shows

this.

Page 3: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

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.

Then press next >

Page 4: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

- Press Next> again

Scroll down the list, press add python.exe to Path, then choose Entire feature will be installed on local hard drive

Page 5: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

Press Next >

Page 6: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

The following icon appears in the programs, from now on we simply double-click this

icon to open the Python IDE, and it is a simple IDE.

The command window, shell, appears:

Page 7: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

There two different modes of programming:

- 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.

- Script Mode Programming (batch mode):

Invoking the interpreter without 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 >>>

Page 8: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

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

Page 9: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

In our lab we will use another IDE (Integrated development environment) called PyCharm Community Edition: First , be sure Python interpreter is downloaded and installed on your computer , then double-click the PyCharm Download file then click Next >

Page 10: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction
Page 11: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

Click on Create New Project:

Page 12: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

Select location project and its name, and sure you select correct Python interpret Then click on Create.

Right Click on the Project File in our example (FirstProject) Then click New → File

Insert a new File Name

Page 13: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

Then select type of file in our case (Python)

PyCharm WorkSpace:

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

Page 14: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

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

Lab Work:

Output of Listing 1.2 (arrow.py) to be

Page 15: Lab1 - Islamic University of Gazasite.iugaza.edu.ps/myazji/files/2016/09/lab1-1.pdf · Lab1 Introduction to Python Lab 1: Introduction to python Eng. Mai Z. Alyazji. Introduction

Exercises:

1) What is an interpreter?

2) What is necessary to execute a Python program?

3) Write a program that displays “Welcome to Python”, “Welcome to Engineering”, and “Programming is fun” (Display three messages).

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