0% found this document useful (0 votes)
2 views18 pages

Lab1 Introduction Python

Lab1 focuses on teaching Python basics through hands-on tasks, including creating Python programs and understanding data types and operators. Students are required to complete three tasks, submit a report in Word format, and ensure they use Python version 3.9. The lab emphasizes the importance of using an Integrated Development Environment (IDE) like PyCharm for coding and provides detailed instructions for installation and project setup.

Uploaded by

Ian Seto
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Lab1 Introduction Python

Lab1 focuses on teaching Python basics through hands-on tasks, including creating Python programs and understanding data types and operators. Students are required to complete three tasks, submit a report in Word format, and ensure they use Python version 3.9. The lab emphasizes the importance of using an Integrated Development Environment (IDE) like PyCharm for coding and provides detailed instructions for installation and project setup.

Uploaded by

Ian Seto
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab1: Python Basics

⚫ Please read through this lab document, and follow the instructions to learn and implement the
Python programs shown below on your own.
⚫ Section 3 has three tasks. Please finish the tasks and write a report according to the
requirements. The report should be a word document (named Lab1_<StudentID>_<Your
Name>.docx).
⚫ After Lab 1, please hand over your report and codes for finishing the tasks via Blackboard
(please put them together in a zip file). The due date for the submission is Jan 28, 2026 (Wed),
23:59 PM.
⚫ For keeping the consistency, please follow the instructions and use Python of version 3.9
throughout all labs and finishing the reports.

1. Before Starting (Please read carefully)


In general, two skills are essential to the mastery of a programming language:

First, you need to know the building blocks of a programming language (e.g., Python) –
the vocabulary (i.e., key words) it uses and the grammar it follows. To become a master of a
programming language, you need to spell the words correctly and organize them into “sentences”
in a meaningful way.

Second, you need to “tell a story”. To write a good story, you combine words and
sentences and convey an idea to the readers. You need to make sure the sentences are
grammatically correct so that the readers could understand them. Also, you need to make sure the
sentences are concise and to the point. A clear structure will make your code neat and effective.

To begin this lab, let us look at a brief history of Python (Wikipedia):

Python is a widely used high-level programming language for general-purpose programming, and
it is created by Guido van Rossum and first released in 1991. As an interpreted language, Python has a

1
design philosophy that emphasizes code readability and a syntax that allows programmers to express
concepts in fewer lines of code than might be used in languages such as C++ or Java.

Guido van Rossum


([Link]

Python features a dynamic type system and automatic memory management. It supports multiple
programming paradigms, including object-oriented, imperative, functional and procedural, and has a large
and comprehensive standard library.

2
2. Using Python in Command Line
Python is an interactive programming language. For example, you can enter Python
statements and execute them in an interactive manner using the Windows Command Prompt tool.
Since the cmd tool on lab machines (in ZS1001/ZN604) are disabled by the administrator, for this
part, we encourage students to test on your own computers. If you have already installed Python
on your Windows platform,

→ For Windows users, open the Command Prompt: Win + R -> Type “cmd” -> [Link]

→ For MacOS users, open Terminal: Command + Space -> Type “Terminal” -> [Link]

→ Then, type “python” in the Command Prompt or Terminal to activate the Python
environment.

→ Under the interactive model, try to type the following statement:

Print(“Hello, /Your Name/’s Python world.”)

By executing the statement, the windows console will print a sentence on your screen that
is similar with the one below:

3
3. Integrated Development Environment (IDE) for Python
Now you’ve learned that simple Python statements can run in a command line console.
However, a real-world Python application is more complicated than a few lines of code, and you
will need tools to organize and test them. An Integrated Development Environment (IDE) is
something that you can rely on. According to Wikipedia:

An Integrated Development Environment (IDE) is a software application that provides


comprehensive facilities to computer programmers for software development. An IDE normally consists of
a source code editor, build automation tools and a debugger. Most modern IDEs have intelligent code
completion. Some IDEs, such as NetBeans and Eclipse, contain a compiler, interpreter, or both; others,
such as SharpDevelop and Lazarus, do not. The boundary between an integrated development environment
and other parts of the broader software development environment is not well-defined. Sometimes a version
control system, or various tools to simplify the construction of a Graphical User Interface (GUI), are
integrated. Many modern IDEs also have a class browser, an object browser, and a class hierarchy
diagram, for use in object-oriented software development.

An IDE allows users to easily write, edit, view, and execute the codes. It also enables you to
efficiently organize different Python scripts and modules. In this course, we use PyCharm as our IDE.
PyCharm is an IDE used in computer programing, specifically for the Python Language (Wikipedia). It
provides a graphical user interface that allows users to write, debug, and execute their Python programs.

4
PyCharm is cross-platform and it supports deployment on operation systems such as Windows, MacOS,
and Linux.

In order to make the IDE work, you first need to specify the Python interpreter with Python of
version 3.9. The interpreter is the program that reads your Python programs and carries out their instructions.

→ Prior to this lab, the default interpreter in the lab machines (in ZS1001/ZN604) could be
“Python27/Arcgis 10.6/[Link]” (or empty). A Python interpreter can “interpret” the Python programs
using the Python (version is 2.7) installed along with ArcGIS 10.6. But we will not use this interpreter,
since Python 2.7 is being phased out and Python code grammar has been changed in 3.x versions.

→ If you are using your own computer without PyCharm or Anaconda, please follow the
instructions by “Lab 1 Python Install [Link]” to install Anaconda and PyCharm.

→ To create a new Python environment of 3.9 version, open ‘Anaconda Navigator’. Select
‘Environment’ panel. Click ‘Create’ on the bottom and select Python of 3.9 version for creation. Please
see “Lab 1 Python Install [Link]”-Section 3 for the very detailed instructions.

→ Open PyCharm, create a new project. In the New Project panel, choose the ‘Custom
Environment’ page and select the ‘Select Existing’. Choose ‘Conda’ in the drop-down menu. And then
choose the 3.9 Python interpreter created in the last step from the list. Please see “Lab 1 Python Install
[Link]”-Section 5 for the very detailed instructions (if you are using a computer in
ZN604/ZS1001, you may refer to the Section 7 rather than Section 5).

5
3.1 Write Your First Python Code in PyCharm

At the beginning of lab, create a new project in PyCharm named Lab1. After setting project
properties, the interface will be like:

→ Then you can create a .py file in this project at File →New→Python File. Note: the extension
of file must be .py.

→ After typing your code in a .py file, you can execute the file by clicking the “Run” on the top

menu and select the option “Run” with in PyCharm.

6
Task 1:

Create a new file named “Lab1_task1.py” under your project folder, and enter the following code
into the file:

print(‘First Python Program for <Your Name>’)

What to Submit:

(1) Change <Your Name> to your real name. In PyCharm, launch the python program you’ve
created and then locate the place that displays the program output. Make a screenshot of the output and save
it into your report; [10 points]

(2) Write a short paragraph about the print Python statement. You can Google it or refer to any
online documents. Use your own words to explain its usage in Python, and explain how it can be useful to
certain applications. Incorporate your paragraph into the same word document. [15 points]

7
3.2 Variables and Python Operators

In this section, you will be asked to understand different data types in Python, and implement an
equation to calculate the area of a circle based on what you have learned.

3.2.1 Data Type

Python provides many data types, including int, float, complex, string, list and dictionary.
However, unlike C++ or another program language, data types in Python do not need to be stated
explicitly. The variable type depends on the value you give. Considering the following statements:

Result:

type() is a useful built-in function, which can return the data type of a variable.

a. String

String is an important data type in Python. There are three ways to initialize a String,

a = ‘name’
a = “name”

8
a = “““name”””
As in Python, all indices are zero-based, which means that the indices of elements are 0, 1, 2 …..
The Nth character is indexed with N-1. To access characters in the String, Python provides the following
methods:

String[N]: returns the character with index N

String[N:M]: returns the characters with index from N to (M-1)

len():returns the length of the String

For understanding String, please try to do as follows:

Result:

b. List

List is another very important data structure in Python, because it can store different types of
variable in a single list. List literals are written within square brackets [].

9
Declaration: List1 = [length], where length is optional

Initialization: List1[index] = value or

List1= [value1, value2, value3 …]

Items in a List can be accessed in a similar way we access characters in strings -- use the len()
function to get the length of List (e.g., the total number of elements in a list) and square brackets [ ] to
access these elements. The index of the first element is 0, NOT 1, same as String. Also, a List can be
updated using the following methods:

• [Link](x): Add a new item to the end of the list, equivalent to a[len(a):] = [x];
• [Link](i,x): Insert an item at the given position. The first argument is the index of the element
before which to insert, so [Link](0, x) inserts at the very beginning of the list,
and [Link](len(a), x) is equivalent to [Link](x).

(Reference: [Link]

For understanding List, please try the following:

10
Result:

c. Dictionary

Another data type worth noting is Dictionary. Dictionaries are sometimes found in other
languages as “associative memories” or “associative arrays”. Unlike List and String, dictionaries are
indexed by keys; strings and numbers can always be keys. It is best to think of a dictionary as an
unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).

Dic = {key1:value1, key2: value2, key3: value3 …}


Or you can declare a Dictionary in this way:
Dic = {}
Dic[‘key1’] = value1;

(Reference: [Link]

For understanding Dictionary, please try to do as follows: (Note: “\n” means insert a new line in the text
at this point.)

11
Result:

12
3.2.2 Python Operators
a. Arithmetic Operators: (Ref: [Link] )

b. Assignment Operators: (Ref: [Link] )

Note that the return value of comparison operators is “True” or” False”, which are two key
words in Python.

13
c. Comparison Operators: (Ref: [Link] )

Note: <> is not supported in Python 3.X

14
Task 2:

Create a python program (named ‘Lab1_task2.py’ under your project folder) that calculates the area
of a circle. Specific requirements are as follows:

• Initialize three variables “Radius”, “PI” and “Area” with float type. (Hint: If you want to initialize
variable as float you can directly use: a = 0.0 in Python script.)
• Then, assign values to the two variables:
Radius = 5.5, PI = 3.1415926
• Create an equation to calculate area of circle. Generally, area of circle = PI*Radius2. You can consider
how to realize this calculation in your code.
• Finally print the area of the circle on screen.

What to Submit:

(1) Make a screenshot of the output, and save it in your Lab1_report. Remember to save your code, so
that they can be evaluated when you turn in the project folder. [15 points]
(2) Write a paragraph explaining the difference between two data structures in Python – List & Tuple.
What are their key differences? You are encouraged to incorporate code examples in your report.
[15 points]
(3) Write another paragraph explaining the difference between dictionary and set, which are also two
important data structures in Python. Give real-world examples on when you will use one data type
over the other. [15 points]
(4) Save those paragraphs into your lab report.

15
4. IF Statement and Loop Statement
In Python, we need to do some conditional judgment. The if statement can help us accomplish this
task. If Grammar show like below:

The 1st line is the conditional statement. If the condition is True, the 2nd line will be executed, if not
the 4th line will be executed.

Nested if/else statements:

For Loop statement:

While Loop statement:

Break and continue are control key words in a loop, showed as in lecture.

16
Example: Continue

Result:

Example: Break

Result:

17
Task 3:

Create a python program (named ‘Lab1_task3.py’ under your project folder). In this task, you
are required to use the function defined in Task2, to calculate the area of a list of circles and check
whether the area is less than 15 or not. (Tip: use for loop and if statement to accomplish this task).

1) List Radius_List = [5.1, 1.3, 0.85, 2.2, 7.583, 25.6]


2) Using for loop to traverse all the items in the list: Radius_List;
3) Using if statement to complete area check:

if Area >= 15 print: the Index of the corresponding Radius and the Area Value
if Area < 15 print: Area Value

Tips: You can write your code following the structure below (replace <your_code> to your code), or DIY
your code.

What to Submit:

(1) Please take a screenshot of the results and save in the lab1_report, remember to save your code, so
that they can be evaluated when you turn in the project folder. [15 points]

(2) Understand the practice of using “indentation” in Python. Write 1-2 paragraphs (preferably with code
examples) to explain why indentation is useful in Python, then save in the lab report. [15 points]

ATTENTION:
You need to submit Lab1_Report and the code script files you’ve created.

Please zip your project folder and the lab report, then submit .zip file to the Blackboard →
Assessments → Lab 1.

18

You might also like