0% found this document useful (0 votes)
6 views8 pages

Lab Session 1 - Introduction To Python

Uploaded by

6c46mhz62j
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)
6 views8 pages

Lab Session 1 - Introduction To Python

Uploaded by

6c46mhz62j
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
You are on page 1/ 8

CSS 1021

Programming Fundamentals

Department of CS and SE

Lab Session 1: Starting Out with Python


Learning Outcomes
After this lab, students will be able to:

 Obtain, install and run the Python interpreter


 Create and execute Python basic programs
 Gain knowledge of Python basic syntax

Getting Python
The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official
website of Python https://www.python.org/. You can download Python documentation from
https://www.python.org/doc/. The documentation is available in HTML, PDF, and PostScript formats.

Installing Python with Anaconda


Anaconda is an open-source distribution for python and R. It is used for data science, machine learning,
deep learning, etc. With the availability of more than 300 libraries for data science, it becomes fairly optimal
for any programmer to work on anaconda for data science.

Installation and Setup


To install Anaconda, go to https://www.anaconda.com/distribution/.

How to Install Python Libraries in Anaconda?


1. Open anaconda prompt and check if the library is already installed or not.

2. Since there is no module named numpy present, we will run the following command to install numpy.
CSS 1021
Programming Fundamentals

Department of CS and SE
3. You will get the window shown in the image once you complete the installation.

4. Once you have installed a library, just try to import the module again for assurance.

5. As you can see, there is no error that we got in the beginning, so this is how we can install various
libraries in anaconda.

Run Python in a Jupyter Notebook


1. Use Anaconda Navigator to launch an application.
CSS 1021
Programming Fundamentals

Department of CS and SE

2. Launch Jupyter Notebook by clicking Jupyter Notebook’s Launch button.


3. This will launch a new browser window (or a new tab) showing the Notebook Dashboard.

4. On the top of the right-hand side, there is a dropdown menu labeled “New”. Create a new Notebook
with the Python version you installed.
5. Rename your Notebook. Either click on the current name and edit it or find rename under File in the
top menu bar. You can name it to whatever you’d like, but for this example, we’ll use
MyFirstAnacondaNotebook.

Write the Program


1. In the first line of the Notebook, type or copy/paste print("Hello Anaconda"). This will output the
following: Hello Anaconda
2. Save your Notebook by either clicking the save and checkpoint icon or select File - Save and
Checkpoint in the top menu. When you save your Python programs, use a .py file extension. This is the
extension that Python files use. Files with a .py extension can be opened and edited with a text editor,
but they require a Python interpreter to run.
CSS 1021
Programming Fundamentals

Department of CS and SE

Run your Program


1. Run your new program by clicking the Run button or press F5.
2. You can also run the program from the terminal or command prompt. To do this, change to the directory
that the file is in (eg, cd path/to/file), then run the following: python3 hello.py
3. You can also use your editor's debugging tools to step through the program, one line at a time. It will
notify you of any errors in your code.

Multi-Line Statements
Statements in Python typically end with a new line.
Example
print (“Kate Austen”)
print (“123 Dharma Lane”)
print (“Asheville, NC 28899”)

Program Output
Kate Austen
123 Dharma Lane
Asheville, NC 28899

Reserved Words
The following list shows the Python keywords. These are reserved words and you cannot use them as
constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.

and exec not

assert finally or

break for pass

class from print

continue global raise


CSS 1021
Programming Fundamentals

Department of CS and SE

def if return

del import try

elif in while

else is with

except lambda yield

Escape Sequences
Escape Sequence Description

\ Backslash and newline ignored

\\ Backslash (\)

\' Single quote (')

\" Double quote (")

\n ASCII Linefeed (LF)

\t Horizontal Tab (TAB)

Example 1
print (“\”Kate Austen\””)
print (“Asheville, \t NC 28899”)

Program Output
“Kate Austen“
Asheville, NC 28899

Example 2
print("line1 \
line2 \
line3")

Program Output
line1 line2 line3

Quotation
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same
type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines. For example, all the following are legal.
CSS 1021
Programming Fundamentals

Department of CS and SE
Example
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

Comments
Comments are notes of explanation that document lines or sections of a program. Comments are part of the
program, but the Python interpreter ignores them. Including comments in programs makes code more
readable for humans as it provides some information or explanation about what each part of a program is
doing.

Single-line Comments
Single-line comments are created simply by beginning a line with the hash (#) character, and they are
automatically terminated by the end of line.

Example
# This program displays a person's name and address.
print (“Kate Austen”)
print (“123 Dharma Lane”)

Program Output
Kate Austen
123 Dharma Lane
Asheville, NC 28899

Multi-line Comments
Comments that span multiple lines – used to explain things in more detail are multi-line comments.
Unfortunately, Python doesn’t have a way to write multiline comments as you can in languages such as C,
Java etc. But you can create multiline comments in Python.

The first way is simply by pressing the return key after each line, adding a new hash mark and continuing
your comment from there:

Example 1

# This is a pretty good example


# of how you can spread comments
# over multiple lines in Python
print (“Kate Austen”)
Program Output
Kate Austen

Another thing you can do is use multiline strings by wrapping your comment inside a set of triple quotes.
CSS 1021
Programming Fundamentals

Department of CS and SE
Example 2
"""
This would be a multiline comment
in Python that spans several lines and
describes your code, your day, or anything you want it to
"""
print (“Kate Austen”)

Program Output
Kate Austen

While this gives you the multiline functionality, this isn’t technically a comment. It’s a string that’s not
assigned to any variable, so it’s not called or referenced by your program. Still, since it’ll be ignored at
runtime and won’t appear, it can effectively act as a comment.

Inline Comments
If a comment is placed on the same line as a statement, it is called an inline comment. Similar to the single
line comment, an inline comment begins with a single hash (# ) sign and followed by a space and comment.
It is recommended that an inline comment should separate from the statement at least two spaces. The
following example demonstrates an inline comment.

Example
print (“Asheville, \t NC 28899”) # Print address with tab escape sequence’.

Program Output
Asheville, NC 28899
CSS 1021
Programming Fundamentals

Department of CS and SE

Exercises
1. Insert the missing part of the code below to output "Hello World".
("Hello World")

2. Write print statements that display your name, class, department and university with inline comments.

3. Write a print statement that displays the following text:


Python's the best!

4. Write a print statement that displays the following text:


The cat said "meow.”

5. Comments in Python are written with a special character, which one?

6. Use a multiline string to make a multi-line comment:


This is a comment
written in
more than just one line
7. Write a Python program to print the following string in a specific format. Add comment in the
program “this is a poem”

You might also like