Lab 1 Introduction To Python Programming
Lab 1 Introduction To Python Programming
Lab 1 Introduction To Python Programming
Code Once
Introduction to Computers
Laboratory Manual
Experiment #1
What is Python?
Python is a widely used high-level programming language for general-purpose
programming, created by Guido van Rossum and first released in 1991. Python is very simple,
yet incredibly powerful programming language. You can use it for writing web and desktop
applications, do scientific computations, create scripts, and more. You can use Python for
almost anything. In this course, you will learn the basics of Python syntax, functions and creating
console apps.
Python is interpreted language, which means that Python code is translated and
executed by an interpreter, one statement at a time.
At first, we write our python source file, a file that has .py extension. Then we use the
python interpreter to interpret and run the program.
Python Editions
Python is now being developed and maintained by a large team of volunteers and is
available for free from the Python Software Foundation. Two versions of Python are currently
coexistent: Python 2 and Python 3. The programs written in Python 3 will not run in Python 2 .
Python 3 is a newer version, but it is not backward-compatible with Python 2. This means that
if you write a program using the Python 2 syntax, it may not work with a Python 3 interpreter.
Python provides a tool that automatically converts code written in Python 2 into syntax Python
3 can use. In this course, we will learn Python 2.
2
Experiment #1: Introduction to Python Programming
Installing Python
1. Go to Python's downloads page.
https://www.python.org/downloads/
2. From the page, choose to download the second edition as illustrated in the figure below.
3. Run the installation wizard that you have just downloaded. At this screen, click "Next".
3
Experiment #1: Introduction to Python Programming
4. In this screen, you can specify the location where Python to be installed. Leave as default
and click on Next.
4
Experiment #1: Introduction to Python Programming
5. Click on "Add python.exe to Path" and choose "Will be installed on local hard drive", then
click Next.
6. The installation process will start. At the end, you'll have Python installed on your
machine and ready to be used.
5
Experiment #1: Introduction to Python Programming
1. To run the command prompt, press Win + R at the same time, then type cmd in the
wizard. Or you can search for cmd from the start menu.
2. In the command prompt print the following command to test python's version.
python --version
3. Press Enter. The following result will be displayed on the screen.
Python 2.7.13
Python programs must be written with a particular structure. The syntax must be
correct, or the interpreter will generate error messages and won't execute the program.
1. Open any text editor of your choice and type in the following code:
# This code prints Hello, World on the screen
print("Hello, World!")
2. Save the file as Test.py, then close it.
3. Open the command line and print the following command to interpret the source code
and run the application.
python Test.py
4. You will notice Hello, World! printed out on the screen, WOW! 😅
• print is a function used to print string on the screen.
6
Experiment #1: Introduction to Python Programming
Installing PyCharm
As may you have noticed, that process is tedious and cumbersome. And we need a more
productive model than that. So, developers around the world use those things called Integrated
Development Environments (IDE) that allows us to type in our code, compile, debug and format
it, everything in one place.
PyCharm Community Edition is the free version of PyCharm, a premier IDE for Python.
In this lab, we are going to use it.
1. Go to the download page and download the latest version of PyCharm Community
Edition.
https://www.jetbrains.com/pycharm/download/
2. After download is complete, installation process is easy and straightforward. Do it
yourself.
• As a feature of being a student 😊, you can get PyCharm Professional Edition, and all other JetBrains products, for free
as long as you are a student. Go to this link https://www.jetbrains.com/student/
7
Experiment #1: Introduction to Python Programming
8
Experiment #1: Introduction to Python Programming
5. Right click on project name, choose new -> Python File and write the name you want
for that file. You will notice a new python file is added.
6. Double click on the file name, and write down the code snippet from the previous
section. Click on Run menu from the menu bar and choose Run to run the program.
File Name
Editing Window
Project Files
Congratulations! You have made a complete Python application. From now on, we will
learn to build useful and complex applications.
# Compute Expression
print((15 * 3 + 2.9) / (17 / 3.2 - 5))
9
Experiment #1: Introduction to Python Programming
153.28
Comments
Comments are text inside of your source code that are ignored by the interpreter.
Comments help the people read the code, which is you most of the time, better understand the
intent, structure and functionality of the program. Also, comments are frequently used to hide
parts of the source code without completely deleting them.
1. Line Comments (#), the interpreter ignores everything until the end of the line. When
the Python interpreter sees #, it ignores all text after # on the same line.
# This is a Line Comment
2. Block Comments (''' … '''), also called paragraph comments, the interpreter ignores
everything between those notations. When it sees ''', it scans for the next ''' and ignores
any text between the triple quotation marks. They can happen in one or multiple lines.
'''
This is a Block Comment
'''
Homework
The best way to teach programming is by example, and the only way to learn programming is
by doing. So, please do it yourself and don’t copy paste from others.
10
Experiment #1: Introduction to Python Programming
4. Write a program that displays the area and perimeter of a rectangle with the width of
4.9 and height of 7.5 using the following formulas:
Good Luck
11