Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

Module 6 Python-Getting-Started

The document provides an overview of how to get started with Python programming. It discusses the objectives of learning Python which are to understand how Python works, identify parts of a Python program, and create programs with input and output. It then defines Python as an interpreted, object-oriented programming language and discusses reasons for its popularity like being easy to learn, useful for web development, and having a supportive community. The document proceeds to explain how Python code is executed by an interpreter and virtual machine. It also describes different ways to run Python code interactively or via scripts and modules.

Uploaded by

ILEENVIRUS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Module 6 Python-Getting-Started

The document provides an overview of how to get started with Python programming. It discusses the objectives of learning Python which are to understand how Python works, identify parts of a Python program, and create programs with input and output. It then defines Python as an interpreted, object-oriented programming language and discusses reasons for its popularity like being easy to learn, useful for web development, and having a supportive community. The document proceeds to explain how Python code is executed by an interpreter and virtual machine. It also describes different ways to run Python code interactively or via scripts and modules.

Uploaded by

ILEENVIRUS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

COMPUTER FUNDAMENTALS AND

PROGRAMMING
PYTHON: GETTING STARTED

A.Y. 2023 - 2024


FIRST SEMESTER
University of Science and Technology of Southern Philippines
OBJECTIVES / INTENDED LEARNING OUTCOMES

At the end of the lesson, students will be able to:

• Students should be able to know how Python


programming language works.
• Students should be able to Identify the parts of a Python
program.
• Students should be able to create a Python programs that
have input and output functionalities.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


INTRODUCTION

Python is an open-source (free) programming language that is

used in web programming, data science, artificial intelligence, and

many scientific applications. Learning Python allows the programmer

to focus on solving problems, rather than focusing on syntax.


WHAT IS PYTHON?

Python is an interpreted, object-oriented, high-level programming language


with dynamic semantics. Its high-level built-in data structures, combined with
dynamic typing and dynamic binding, make it very attractive for Rapid
Application Development, as well as for use as a scripting or glue language to
connect components together.

It was created by Guido van Rossum, and released in 1991.

Guido van Rossum

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


10 REASONS WHY PYTHON

• Python is Easy to Learn and Use


• Python is Handy for Web Development Purposes
• The Language is Extensively used in Data Science
• Has Multiple Libraries and Frameworks
• Python can be used in Machine Learning (ML) tool
• Python for Academics
• Has a Highly Supportive Community
• Flexibility and Reliability
• Python Automates Tasks
• The First-choice Always

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


1.1 Introducing the Python Interpreter

An interpreter is a kind of
program that executes other
programs. When you write a
Python program, the Python
interpreter reads your
program and carries out the
instructions it contains. In
effect, the interpreter is a
layer of software logic
between your code and the
computer hardware on your
machine.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


1.1 Introducing the Python Interpreter

Python installation details vary by platform:

• Windows users fetch and run a self-installing executable file that


puts Python on their machines. Simply double-click and say Yes
or Next at all prompts.
• Linux and Mac OS X users probably already have a usable
Python preinstalled on their computers—it’s a standard
component on these platforms today.
• Some Linux and Mac OS X users (and most Unix users) compile
Python from its full source code distribution package

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


1.1 Introducing the Python Interpreter

• Linux users can also find RPM files, and Mac OS X users
can find various Mac specific installation packages.

• Other platforms have installation techniques relevant to


those platforms. For instance, Python is available on cell
phones, tablets, game consoles, and iPods, but
installation details vary widely.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Interpreter Vs Compiler

INTERPRETER COMPILER
Scans the entire program and translates it as a
Translates program one statement at a time.
whole into machine code.
Interpreters usually take less amount of time Compilers usually take a large amount of time
to analyze the source code. However, the to analyze the source code. However, the
overall execution time is comparatively slower overall execution time is comparatively faster
than compilers. than interpreters.

Generates Object Code which further


No Object Code is generated, hence are
requires linking, hence requires more
memory efficient.
memory.
Programming languages like JavaScript, Programming languages like C, C++, Java use
Python, Ruby use interpreters. compilers.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Interpreter Vs Compiler

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Program Execution

What it means to write and run a Python script depends on


whether you look at these tasks as a programmer, or as a
Python interpreter. Both views offer important perspectives
on Python programming.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Byte code compilation

When we execute a source code (a file with


a .py extension), Python first compiles it into
a bytecode. The bytecode is a low-level
platform-independent representation of
your source code; however, it is not the
binary machine code and cannot be run by
the target machine directly. In fact, it is a
set of instructions for a virtual machine
which is called the Python Virtual
Machine (PVM).

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


The Python Virtual Machine (PVM)

Python Virtual Machine (PVM) is a program which provides


programming environment. The role of PVM is to convert the
byte code instructions into machine code so the computer can
execute those machine code instructions and display the output.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON PROGRAMS

Before proceeding with this chapter Python should be installed. See the
following for the installation of Python into your system:
 Installing Python (click to install Python 3.11.3)

This section will guide you through a series of ways to run Python
scripts, depending on your environment, platform, needs, and skills as
a programmer.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


SCRIPTS VS MODULES

Scripts are processed by some kind of interpreter, which is responsible for


executing each command sequentially. A plain text file containing Python code
that is intended to be directly executed by the user is usually called script, which
is an informal term that means top-level program file. On the other hand, a plain
text file, which contains Python code that is designed to be imported and used
from another Python file, is called module.

So, the main difference between a module and a script is that modules are
meant to be imported, while scripts are made to be directly executed.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


WHAT’S THE PYTHON INTERPRETER?

Interpreter is the program you’ll need to run Python


code and scripts. Technically, the interpreter is a
layer of software that works between your program
and your computer hardware to get your code
running.

The interpreter is able to run Python code in two


different ways:
• As a script or module
• As a piece of code typed into an interactive
session

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON CODE INTERACTIVELY

A widely used way to run Python code is through an interactive session. To start a
Python interactive session, just open a command-line or terminal and then type in
python, or python3 depending on your Python installation, and then hit Enter.

Here’s an example of how to do this on Linux:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON CODE INTERACTIVELY

The standard prompt for the interactive mode is >>>, so as soon as


you see these characters, you’ll know you are in.

Now, you can write and run Python code as you wish, with the only
drawback being that when you close the session, your code will be
gone.

When you work interactively, every expression and statement you type
in is evaluated and executed immediately:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON CODE INTERACTIVELY

An interactive session will allow you to test every piece of


code you write, which makes it an awesome development
tool and an excellent place to experiment with the language
and test Python code on the fly.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON CODE INTERACTIVELY

To exit interactive mode, you can use one of the following options:

• quit() or exit(), which are built-in functions


• The Ctrl+Z and Enter key combination on Windows, or just Ctrl+D on
Unix-like systems

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON CODE INTERACTIVELY

If you’ve never worked with the command-line or terminal, then you


can try this:
 On Windows, the command-line is usually known as command
prompt or MS-DOS console, and it is a program called cmd.exe. The
path to this program can vary significantly from one system version to
another.

A quick way to get access to it is by pressing the Win+R key


combination, which will take you to the Run dialog. Once you’re
there, type in cmd and press Enter.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON CODE INTERACTIVELY

On GNU/Linux (and other Unixes), there are several applications that give you access to the

system command-line. Some of the most popular are xterm, Gnome Terminal, and Konsole.

These are tools that run a shell or terminal like Bash, ksh, csh, and so on.

In this case, the path to these applications is much more varied and depends on the

distribution and even on the desktop environment you use. So, you’ll need to read your

system documentation.

On Mac OS X, you can access the system terminal from Applications → Utilities → Terminal.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW DOES THE INTERPRETER RUN PYTHON SCRIPTS?

When you try to run Python scripts, a multi-step process begins. In this process the
interpreter will:

1. Process the statements of your script in a sequential fashion


2. Compile the source code to an intermediate format known as bytecode
3. Ship off the code for execution.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


How to Run Python Scripts Using the Command-Line

A Python interactive session will allow you to write a lot of


lines of code, but once you close the session, you lose
everything you’ve written. That’s why the usual way of writing
Python programs is by using plain text files. By convention,
those files will use the .py extension. (On Windows systems the
extension can also be .pyw.)

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


How to Run Python Scripts Using the Command-Line

Python code files can be created with any plain text editor. Open
any preferred text editor and write the following code:

Save the file in your working directory with the file name extension “.py”.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON SCRIPTS FROM AN IDE OR A TEXT EDITOR

When developing larger and more complex applications, it is


recommended that you use an integrated development environment
(IDE) or an advanced text editor.
Most of these programs offer the possibility of running your
scripts from inside the environment itself. It is common for them
to include a Run or Build command, which is usually available from
the tool bar or from the main menu.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


HOW TO RUN PYTHON SCRIPTS FROM AN IDE OR A TEXT EDITOR

Python’s standard distribution includes IDLE as the default IDE, and you
can use it to write, debug, modify, and run your modules and scripts.

Other IDEs such as Eclipse-PyDev, PyCharm, Eric, and NetBeans also


allow you to run Python scripts from inside the environment.

Advanced text editors like Sublime Text and Visual Studio Code also
allow you to run your scripts.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS

We use Python assignment statements to assign objects to

names. The target of an assignment statement is written on the

left side of the equal sign (=), and the object on the right can be

an arbitrary expression that computes an object.

Discussion Reference: https://www.geeksforgeeks.org/different-

forms-of-assignment-statements-in-python/?ref=lbp

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS

There are some important properties of assignment in Python :

• Assignment creates object references instead of copying

the objects.

• Python creates a variable name the first time when they

are assigned a value.

• Names must be assigned before being referenced.

• There are some operations that perform assignments

implicitly.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

1. Basic form:
This form is the most common form.

Output:

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

2. Tuple Assignment:

When we code a tuple on the left side of the =,


Python pairs objects on the right side with targets on
the left by position and assigns them from left to right.
Therefore, the values of x and y are 50 and 100
respectively.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

4. Sequence Assignment:

In recent version of Python, tuple and list


assignment have been generalized into instances of
what we now call sequence assignment – any
sequence of names can be assigned to any
sequence of values, and Python assigns the items
one at a time by position.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

5. Extended Sequence Unpacking:

Here, p is matched with the first character in the


string on the right and q with the rest. The starred
name (*q) is assigned a list, which collects all items in
the sequence not assigned to other names.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: ASSIGNMENT STATEMENTS FORMS

Program control statements, mainly:


• Conditional Statements
• Loops
• Break
• Continue

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONDITIONAL STATEMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
LOOPS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
LOOPS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
CONTINUE STATEMENT

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PROGRAM CONTROL STATEMENTS:
BREAK STATEMENT

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


END OF PRESENTATION
Thank you for listening!

Any questions?
Nothing is impossible. The word itself says ‘I’m possible!'

— Audrey Hepburn

You might also like