0% found this document useful (0 votes)
3 views16 pages

Python CW Module1 - Session2

Uploaded by

musamujahid397
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)
3 views16 pages

Python CW Module1 - Session2

Uploaded by

musamujahid397
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

STUDY SESSION 1.

2
Python Development Environment, Variables, and Operators
Section and Subsection Headings
Introduction
1.0 Learning Outcome
2.0 Main Content
2.1- Python program development environment
2.2- Writing Your First Hello World Program on Python
2.3- Variables
2.4- Operators
3.0 Conclusion/Summary
4.0 Self-Assessment Questions
5.0 Additional Activities (Videos, Animations & out of Class Activities)
6.0 Reference/Further Reading
Introduction
This session introduces you to Python development environment where you will learn how to
download and install the interpreter. You will then learn how to code your first Python “Hello
World” using the three methods for writing and running programs in Python. The Integrated
Development Environment for a more intuitive and seamless program development in Python is
discussed with a particular reference to downloading and installing PyCharm IDE. You will learn
Python identifier and your first sets of Python command/Keywords needed for making usable
statements and running application. The Variables and Operators as used in Python is discussed in
this study session to prepare you for the next section that deals with complete programs that solve
defined problems.

1.0 Study Session Specific Learning Outcomes

After this study session, you are expected to be able to:

1. Be familiar with Python Program Development Environment.


2. Understand how to download and Install Python Interpreter and configure the running
environment
13
3. Install an IDE and Run simple Python program
4. Understand python variables, and operators

2.1 Python Program Development Environment

Python is available on a wide variety of platforms including Windows, Linux and Mac OS X. Let's
understand how to set up our Python environment.

2.1.1 Obtaining Python 3 Interpreter

There are many versions of python but for this course we are working with python 3. To download
the working version of python you should visit the official website. The most up-to-date and
current source code, binaries, documentation, news, etc., is available on the official website of
Python [Link]

Python download window

You can download Python documentation from [Link] The


documentation is available in HTML, PDF, and PostScript formats.

2.1.2 Installing Python

Python distribution is available for a wide variety of platforms. You need to download only the
binary code applicable for your platform and install Python.

14
If you have a modern and up-to-date Mac or other Unix-like system you may already have Python
3 installed. You can check by typing python -V (note the capital V) in a console ([Link] on
Mac OS X)—if the version is 3.x you’ve already got Python 3 and don’t have to install it yourself.

For installing Python on Windows and Mac OS X, easy-to-use graphical installer packages are
provided that take you step-by-step through the installation process. These are available from
[Link]/download as earlier stated.

Windows Installation of Python

Here are the steps to install Python on Windows machine.

 Open a Web browser and go to [Link]

 Follow the link for the Windows installer [Link] file where X is the version you
need to install.

 To use this installer [Link], the Windows system must support Microsoft Installer
2.0. Save the installer file to your local machine and then run it to find out if your machine
supports MSI.

 Run the downloaded file. This brings up the Python install wizard, which is really easy to
use. Just accept the default settings, wait until the install is finished, and you are done.
Following the following steps

Step 1) Once the download is completed, run the .exe file to install Python. Click on Install Now.

Installation window
15
Step 2) You can see Python installing progress bar at this point.

Step 3) When it finishes, you can see a screen that says the Setup was successful. Now click on
“Close”.

Setting up PATH

Programs and other executable files can be in many directories, so operating systems provide a
search path that lists the directories that the OS searches for executables.

The path is stored in an environment variable, which is a named string maintained by the operating
system. This variable contains information available to the command shell and other programs.

The path variable is named as PATH in Unix or Path in Windows (Unix is case sensitive;
Windows is not).
16
In Mac OS, the installer handles the path details. To invoke the Python interpreter from any
particular directory, you must add the Python directory to your path.

Setting path at Windows


To add the Python directory to the path for a particular session in Windows −
At the command prompt − type path %path%;C:\Python and press Enter.
Note − C:\Python is the path of the Python directory

Running Python
There are three different ways to start Python −
(i) Interactive Interpreter
You can start Python from Unix, DOS, or any other system that provides you a command-line
interpreter or shell window.
Enter python on the command line.
Start coding right away in the interactive interpreter.

Python code in interactive mode

(ii) Script from the Command-line


A Python script can be executed at command line by invoking the interpreter on your application.
For instance, a code written and saved as [Link] can be executed in the command line as in
following:

Running script from command line

17
(iii) Integrated Development Environment
You can run Python from a Graphical User Interface (GUI) environment as well, if you have a
GUI application on your system that supports Python. PythonWin is the first Windows interface
for Python and is an IDE with a GUI. There are many other available IDE you can download to
use for developing your Python applications. These are IDLE, Sublime, Atom, Thorny, Vim and
PyCharm. However, we will use PyCharm as the IDE for developing and running our codes
throughout this course.

2.1.3 Installing PyCharm

PyCharm
PyCharm is a cross-platform editor developed by JetBrains. PyCharm provides all the tools you
need for productive Python development. The vast number of PyCharm features help make
PyCharm a great Python IDE for beginners. Below are the detailed steps for installing PyCharm.

Here is a step by step process on how to download and install Pycharm IDE on Windows:

Step 1) To download PyCharm visit the website

[Link] and Click the “DOWNLOAD” link under the


Community Section.

Step 2) Once the download is complete, run the exe for install PyCharm. The setup wizard should
have started. Click “Next”.

18
Step 3) On the next screen, Change the installation path if required. Click “Next”.

Step 4) On the next screen, you can create a desktop shortcut if you want and click on “Next”.

19
Step 5) Choose the start menu folder. Keep selected JetBrains and click on “Install”.

Step 6) Wait for the installation to finish.

Step 7) Once installation finished, you should receive a message screen that PyCharm is installed.
If you want to go ahead and run it, click the “Run PyCharm Community Edition” box first and
click “Finish”.

20
Step 8) After you click on “Finish,” the Following screen will appear.

2.2 Writing Your First Hello Word Program on PyCharm

In this course, we are going to get familiar with PyCharm as our development environment. To
write any program lunch PyCharm by clicking on the Icon from the desktop or trace where the
[Link] is stored and load from there.

2.2.1 PyCharm Coding Environment


Below is the sample PyCharm program development window

Code area

Run
Window

PyCharm Coding Screen

How to Perform Some Important Activities


To Run a Program
- Click on Run on the Menu Bar
- Click Run or
- Shift + F10 from the Keyboard
Working on a Python Project

21
You may need to create a new project of continue on an
existing project. To create a new project
- Click File from Menu Bar
- Click New Project
- Change the items on the new project window

To Save a Program

- Click File from Menu Bar


- Click Save As or Save all as the case may be
- Or Ctrl + S

To Change the Outlook of PyCharm in terms of background color and fonts

- Click File from Menu Bar


- Click Settings
- Select your convenient scheme and click Apply

Changed PyCharm color scheme

22
2.3 Python Variables

Variables are locations in the computer memories for storing data. It can be looked at as containers
for storing data values. Just like containers for storing items are labelled for identification purpose,
variables must be named for identification and usage of the data stored in them.

Creating Variables

Python has no command for declaring a variable. A variable is created the moment you first assign
a value to it.

Example

x = 5
y = "John"
print(x)
print(y)

Variables do not need to be declared with any particular type, and can even change type
after they have been set.

Example

x = 4 # x is of type int
print(x)
print(type(x))
x = "Sally" # x is now of type str
print(x)
print(type(x))

2.3.1 Rules for Forming Variable Names

 Should be meaningful
 Must start only with alphabet or underscore
 Can contain alphabet, numbers and underscore
 Variable names are case sensitive

23
2.4 Python Operators
Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

Python Arithmetic Operators

Arithmetic operators are used to perform mathematical computation on values:

Name Operator Example of Operator on x=2 and y=3

Addition + x+y=5 # addition of x and y

Subtraction - x – y = -1 # subtract y from x

Multiplication * x*y=6 # multiply x by y

Division / x / y = 0.66 # divide x by y

Modulus % x%y=2 # remainder of division of x by y

Exponential ** x ** y = 8 # x raised to the power of y

Floor Division // x // y = 0 # returns the whole number of a division

Python Assignment Operators

Assignment operators are used to assign values to variables:

24
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Python Comparison Operators

Comparison operators are used to compare two values. They act on ordinary values and return
logical results (i.e. True or False results)

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

25
Operator Description Example

in Returns True if a sequence with the specified value is present in the x in y


object
not in Returns True if a sequence with the specified value is not present in x not in y
the object

Python Logical Operators

Logical operators are used to combine conditional statements. They act on logical values (True or
False) and return logical result (i.e. True or False results):

Operator Description Example


and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the
same object, with the same memory location:

Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:

Operator Description Example


is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y

26
Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

Operator Name Description


& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let
the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in
from the left, and let the rightmost bits fall off

3.0 Conclusion/Summary
In this study session, you have been introduced to Python programming development environment,
i.e. Integrated Development Environment (IDE) for developing Python was introduced. You were
guided to download the Interpreter from the [Link] site and install. The environment for
developing and running Python program was explained and you learnt how to download the
community edition of PyCharm from JetBrains website as the standard IDE for developing and
running Python programs in this class. You are also introduced to what variable means in Python
and the rules for forming variable names. Finally, the seven recognizable operators in Python were
introduced and described.

In-text Questions
Why Do you require an IDE for developing Python Application?
In-text Answers
IDE helps to simplify coding wherein features such as autocomplete will help to easily complete keywords and
help auto corrections before running. It has auto debug features to assist the writer. Its graphical features help
setting for an appealing color combination for the writer.

27
4.0 Self-Assessment Questions
1. What are the rules for forming Python Variable names?
2. Write Python Program for adding numerical values and display the output
3. What is the output of the following program,
# sample code
var1 = 5
var2 = 3
var3 = var1%var2
var4 = var1//var3
print(var3)
print(var3)

5.0 Additional Activities

Install your interpreter and the IDE in your computer and obtain the mobile version to install in
your smart phone for easy test-runs

Set up the environment variables in your system if its not automatically set during installation.

Visit You-tube on [Link]

[Link]

6.0 Reference/Further Reading

David, A., Dan, B., Joanna, J., & Fletcher, H. (2020). Python Basics: A Practical Introduction to
Python (S. Jacob (ed.); 4th ed.).
Summerfield, M. (2010). Programming in Python 3: A Complete Introduction to the Python
Language (Second). Addison-Wesley Professional.

28

You might also like