You are on page 1of 32

Programming with

Python

Apr 4, 2024 Python Programming 1


Objectives
Upon completing this session, students will be
able to:

1.Discuss the history, nature and characteristics of


the Python programming language

2.Install and work with the Python development


environment.

3.Create a simple Python application

Apr 4, 2024 Python Programming 2


What is

?
Apr 4, 2024 Python Programming 3
What is Python?
• Python is a very popular general-purpose
interpreted, interactive, object-oriented, and
high-level programming language.
• Python is dynamically-typed and garbage-
collected programming language.
• Python supports multiple programming
paradigms, including Procedural, Object
Oriented and Functional programming
language.

Apr 4, 2024 Python Programming 4


History of Python
• It was created by Guido van Rossum during
1985- 1990.
• Python was developed at the National
Research Institute for Mathematics and
Computer Science in the Netherlands.
• Python is derived from many other
languages, including ABC, Modula-3, C, C++,
Algol-68, SmallTalk, and Unix shell and other
scripting languages.
Apr 4, 2024 Python Programming 5
Python Features
• Easy-to-learn − Python has few keywords,
simple structure, and a clearly defined syntax.
This allows the student to pick up the language
quickly.
• Easy-to-read − Python code is more clearly
defined and visible to the eyes.
• Easy-to-maintain − Python's source code is
fairly easy-to-maintain.
• A broad standard library − Python's bulk of the
library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.

Apr 4, 2024 Python Programming 6


• Interactive Mode − Python has support for an
interactive mode which allows interactive testing and
debugging of snippets of code.
• Portable − Python can run on a wide variety of
hardware platforms and has the same interface on all
platforms.
• Extendable − You can add low-level modules to the
Python interpreter. These modules enable
programmers to add to or customize their tools to be
more efficient.
• Databases − Python provides interfaces to all major
commercial databases.

Apr 4, 2024 Python Programming 7


• GUI Programming − Python supports GUI
applications that can be created and ported
to many system calls, libraries and windows
systems, such as Windows MFC, Macintosh,
and the X Window system of Unix.
• Scalable − Python provides a better structure
and support for large programs than shell
scripting.

Apr 4, 2024 Python Programming 8


Environment Setup
• Python is available on a wide variety of
platforms including Linux and Mac OS X.
• 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.

Apr 4, 2024 Python Programming 9


Integrated Development
Environment
• You can run Python from a Graphical User
Interface (GUI) environment
• Unix − IDLE is the very first Unix IDE for Python.
• Windows − PythonWin is the first Windows interface for
Python and is an IDE with a GUI.
• Macintosh − The Macintosh version of Python along
with the IDLE
• IDE is available from the main website,
downloadable as either MacBinary or
BinHex'd files.
Apr 4, 2024 Python Programming 10
• IDLE is an Integrated Development
Environment for Python, typically used on
Windows
• Multi-window text editor with syntax
highlighting, auto-completion, smart indent
and other.
• Python shell with syntax highlighting.
• Integrated debugger with stepping,
persistent breakpoints, and call stack
visibility

Apr 4, 2024 Python Programming 11


Apr 4, 2024 Python Programming 12
The Hello World Application
Source Code:

Apr 4, 2024 Python Programming 13


Python - Basic Syntax

Apr 4, 2024 Python Programming 14


Basic Syntax
• The Python syntax defines a set of rules that
are used to create Python statements while
writing a Python Program.
• The Python Programming Language Syntax
has many similarities to Perl, C, and Java
Programming Languages

Apr 4, 2024 Python Programming 15


Python Identifiers
• A Python identifier is a name used to identify
a variable, function, class, module or other
object.
• An identifier starts with a letter A to Z or a to
z or an underscore (_) followed by zero or
more letters, underscores and digits (0 to 9).
• Python does not allow punctuation
characters such as @, $, and % within
identifiers.
Apr 4, 2024 Python Programming 16
Naming conventions for Python
identifiers
• Python Class names start with an uppercase letter. All
other identifiers start with a lowercase letter.
• Starting an identifier with a single leading underscore
indicates that the identifier is private identifier.
• Starting an identifier with two leading underscores
indicates a strongly private identifier.
• If the identifier also ends with two trailing
underscores, the identifier is a language-defined
special name.

Apr 4, 2024 Python Programming 17


Python Reserved Words
and as assert
break class continue
def del elif
else except False
finally for from
global if import
in is lambda
None nonlocal not
or pass raise
return True try
while with yield
Apr 4, 2024 Python Programming 18
Python Lines and Indentation
• Python programming provides no braces to
indicate blocks of code for class and function
definitions or flow control.
• Blocks of code are denoted by line
indentation, which is rigidly enforced.
• The number of spaces in the indentation is
variable, but all statements within the block
must be indented the same amount

Apr 4, 2024 Python Programming 19


Apr 4, 2024 Python Programming 20
Python Multi-Line Statements
• Statements in Python typically end with a
new line.
• Python allow the use of the line continuation
character (\) to denote that the line should
continue.

Apr 4, 2024 Python Programming 21


Quotations in Python
• 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.

Apr 4, 2024 Python Programming 22


Comments in Python
• Comments are added with the purpose of making the
source code easier for humans to understand, and are
ignored by Python interpreter
• A hash sign (#) that is not inside a string literal begins a
comment.
• Triple-quoted string can be used as a multiline comments

Apr 4, 2024 Python Programming 23


Python – Variables

Apr 4, 2024 Python Programming 24


Variables
• Python variables are the reserved memory
locations used to store values with in a
Python Program.
• Based on the data type of a variable, Python
interpreter allocates memory and decides
what can be stored in the reserved memory.

Apr 4, 2024 Python Programming 25


Creating Python Variables
• A Python variable is created automatically
when you assign a value to it.
• The equal sign (=) is used to assign values to
variables.
• The operand to the left of the = operator is
the name of the variable and the operand to
the right of the = operator is the value stored
in the variable

Apr 4, 2024 Python Programming 26


Printing Python Variables
• Once we create a Python variable and assign
a value to it, we can print it using print()
function.

Apr 4, 2024 Python Programming 27


Delete a Variable
• You can delete the reference to a number
object by using the del statement.
• The syntax of the del statement is

Apr 4, 2024 Python Programming 28


Python Variable Names
• Every Python variable should have a unique
name like a, b, c.
• A variable name can be meaningful like color,
age, name etc.
• There are certain rules which should be taken
care while naming a Python variable:
• It must start with a letter or the underscore
character

Apr 4, 2024 Python Programming 29


• It cannot start with a number or any special
character like $, (, * % etc.
• It can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Python variable names are case-sensitive which
means Name and NAME are two different
variables in Python.
• Python reserved keywords cannot be used
naming the variable.

Apr 4, 2024 Python Programming 30


Python Local Variable
• Python Local Variables are defined inside a
function.
• We can not access variable outside the
function

Apr 4, 2024 Python Programming 31


Python Global Variable
• Any variable created outside a function can
be accessed within any function and so they
have global scope.

Apr 4, 2024 Python Programming 32

You might also like