2. What is Python?
⚫Python isaclear andpowerfulobject-oriented programming
language,comparable to Perl, Ruby,Scheme,or Java.
**
2
3. History
⚫Python is a high-level, interpreted scripting language developed
in the late 1980s by Guido van Rossum at the National Research
Institute for Mathematics and Computer Science in the
Netherlands.
⚫The initial version was published at the alt.sources newsgroup in
1991, and version 1.0 wasreleased in 1994.
**
3
4. ⚫Python 2.0 was released in 2000, and the 2.x versions were
the prevalent releasesuntil December 2008.
⚫At that time, the development team made the decision to
release version 3.0, which contained a few relatively small
but significant changes that were not backward compatible
with the 2.x versions.
⚫Python 2 and 3 are very similar, and some features of Python
3 have been backported to Python 2. But in general, they
remain not quite compatible.
(Source: https://realpython.com/python-introduction/)
**
4
5. ⚫The name Python, by the way, derives not from the snake,
but from the British comedy troupe Monty Python’s Flying
Circus,of which Guido was,and presumably still is, afan.
**
5
6. Some of Python's notable features:
⚫ Uses an elegant syntax, making the programs you write easier to read.
⚫ Is an easy-to-use language that makes it simple to get your program working.
⚫ Comes with a large standard library that supports many common
programming tasks such as connecting to web servers, searching text with
regular expressions, reading and modifying files.
⚫ Python's interactive mode makes it easy to test short snippets of code.There's
also abundled development environment called IDLE.
⚫ Is easily extended by adding new modules implemented in a compiled
language such asC or C++.
(Source: https://wiki.python.org/moin/BeginnersGuide/Overview)
**
6
7. ⚫ Can also be embedded into an application to provide a programmable
interface.
⚫ Runs anywhere, including Mac OS X, Windows, Linux, and Unix,
with unofficialbuilds also availableforAndroid and iOS.
⚫ Is free software in two senses. It doesn't cost anything to download or
use Python, or to include it in your application.
⚫ Python can also be freely modified and re-distributed, because while the
language is copyrighted it's available under an open source license.
(Source:https://wiki.python.org/moin/BeginnersGuide/Overview)
7
8. Some programming-language features:
⚫ A variety of basic data types are available: numbers (floating point, complex, and
unlimited-length long integers), strings (bothASCIIand Unicode),lists,and dictionaries.
⚫ Python supports object-oriented programming with classes and multiple
inheritance.
⚫ Code can be grouped into modules and packages.
⚫ The language supports raising and catching exceptions, resulting in cleaner error
handling.
⚫ Data types are strongly and dynamically typed. Mixing incompatible types (e.g.
attempting to add a string and a number) causes an exception to be raised, so errors are
caught sooner.
⚫ Python contains advanced programming features such as generators and list
comprehensions.
⚫ Python's automatic memory management frees you from having to manually
allocate and free memoryin your code.
(Source: https://wiki.python.org/moin/BeginnersGuide/Overview)
8
9. Module 1:
⚫Chapter 1:Whyshould you learn to write programs
⚫Chapter 2:Variables,expressions and statements
⚫Chapter 3: Conditional execution
⚫Chapter 4: Functions
9
10. Chapter 1:
Whyshould you learn to write programs
⚫1.1 Creativity and motivation
⚫1.2 Computer hardware architecture
⚫1.3 Understanding programming
⚫1.4W
ords and sentences
⚫1.5 Conversingwith Python
⚫1.6Terminology:interpreter and compiler
⚫1.7Writing aprogram
⚫1.8What is aprogram?
⚫1.9The building blocksof programs
⚫1.10What could possibly go wrong?
10
14. Terminology:
Interpreter and Compiler
⚫Python is a high-level language intended to be relatively
straightforward for humans to read and write and for computers to
read and process. The actual hardware inside the Central Processing
Unit (CPU) does not understand anyof these high-level languages.
⚫The CPU understands alanguage called machine language.
⚫Since machine language is tied to the computer hardware, machine
language is not portableacross different typesof hardware.
14
15. Translator
⚫Allows programmers to write in high-level languages like Python
or JavaScript and convert the programs to machine language for
actual execution bythe CPU.
⚫These programming language translators fall into two general
categories:
(1) interpreters and
(2) compilers.
⚫Programs written in high-level languages can be moved between
different computers by using a different interpreter on the new
machine or recompiling the code to create a machine language
version of the program for the new machine
15
16. 1) Interpreter:
⚫ An interpreter reads the source code of the program as written by the
programmer, parses the source code,and interprets the instructions on the fly.
⚫ Python is an interpreter and when we are running Python interactively, we can
type a line of Python (a sentence) and Python processes it immediately and is
readyfor us to type another line of Python.
>>> x = 6
>>> print(x)
6
>>> y = x * 7
>>> print(y)
42
⚫ Even though we are typing these commands into Python one line at a time,
Python is treating them as an ordered sequence of statements with later
statements able to retrieve data created in earlier statements. We are writing our
first simple paragraph with four sentences in a logical and meaningful order. It is
the nature of an interpreter to be able to have an interactive conversation as shown
above.
16
17. 2) Compiler:
⚫Compilers needs to be handed the entire program in a file, and
then it runs a process to translate the high-level source code into
machine language and then the compiler puts the resulting machine
language into afile for later execution.
⚫If you have a Windows system, often these executable machine
language programs have a suffix of “.exe” or “.dll” which stand for
“executable” and “dynamic link library” respectively. In Linux and
Macintosh,there is no suffix that uniquely marks afile asexecutable.
**
17
18. ⚫The Python interpreter is written in a high-level language
called“C”.
⚫So Python is a program itself and it is compiled into machine
code. When you installed Python on your computer (or the
vendor installed it), you copied a machine-code copy of the
translated Python program onto your system. In Windows,
the executable machine code for Python itself is likely in a
file with aname like: C: Python35 python.exe
⚫Click
18
19. General types of errors
⚫Syntax error: means that you have violated the “grammar” rules
of Python.
⚫Logic errors: Alogic error is when your program has good syntax
but there is a mistake in the order of the statements or perhaps a
mistake in how the statements relate to one another
⚫Semantic errors: A semantic error is when your description of
the steps to take is syntactically perfect and in the right order, but
there is simply a mistake in the program. The program is perfectly
correct but it does not do what youintended for it to do.
19
20. The building blocks of programs
⚫ Input: Get data from the “outside world”. This might be reading data from a
file, or even some kind of sensor like a microphone or GPS. In our initial
programs,our input will come from the user typing data on the keyboard.
⚫ Output: Display the results of the program on a screen or store them in a file
or perhaps write them to adevice like aspeaker to playmusic or speak text.
⚫ Sequential execution: Perform statements one after another in the order
theyare encountered in the script.
⚫ Conditional execution: Check for certain conditions and then execute or
skip asequence of statements.
⚫ Repeated execution: Perform some set of statements repeatedly, usually
with some variation.
⚫ Reuse: Write a set of instructions once and give them a name and then reuse
those instructions asneeded throughout your program.
20
21. Writinga program
⚫Script: When we want to write a program, we use a text
editor to write the Python instructions into a file, which is
called ascript.
⚫Byconvention,Python scripts havenames that end with .py
⚫To execute the script, you have to tell the Python interpreter
the name of the file.
21
22. Installing the Python
⚫Before you can converse with Python, you must first install
the Python software on your computer and learn how to
start Python on your computer.
⚫https://www.python.org/downloads/
⚫https://www.anaconda.com/distribution/
22
25. Python Shell
⚫Python is most commonly translated byuse of an interpreter.
⚫Thus python provides the very useful ability to execute in
interactive mode.
⚫The window that provides this interaction is referred to as
the python shell.
⚫Although working in the python shell is convenient, the
entered code is not saved.
25
27. IDLE
⚫Integrated Development Environment is a bundle set of
software tools for program development.
⚫This typicallyincludes
⚫Editor: for creating and modifying programs
⚫Translator: for executing programs
⚫Program debugger: takes control of the execution of a program
to aidin finding program errors
**
27
30. ⚫At some point, you will be in a terminal or command window
and you will type python or python3 and the Python interpreter
will start executing in interactive mode and appear somewhat as
follows:
**
30
31. ⚫The >>> (chevron) prompt is the Python interpreter’s way of
asking you, “What do you want me to do next?” Python is ready
to have aconversation with you.
>>> quit()
⚫The proper way to say “good-bye” to Python is to enter quit() at
the interactive chevron >>> prompt.
**
31
32. ⚫In aUnix or Windows command window, you would type
python hello.py as follows:
csev$ cat hello.py
print('Hello world!')
csev$ python hello.py
Hello world!
csev$
**
32
37. The Python Standard Library
⚫The Python Standard Library is a collection of built-in
modules, each providing specific functionalities beyond what
is included in the core part of Python.
⚫In order to utilize the capabilities of a given module in a
specific program, an import statement is used.
**
37
39. False None True and as
assert async await break
class continue def del
elif else except finally
for from global if
import in is lambda
nonlocal not or pass
raise return try while
with yield
**
39
44. Variable names and keywords
False None True and as
assert async await break
class continue def del
elif else except finally
for from global if
import in is lambda
nonlocal not or pass
raise return try while
with yield
50. Asking the user for input
a=input()
print(a)
b=input(‘enter the number:’)
print(b)
p=‘what is the speed of bike’
speed=input(p)
print(int(speed)+10)
51. Chapter 3
Conditional Execution
• Boolean expressions
• Logical expressions
• Conditional execution
• Alternative execution
• Chained conditionals
• Nested conditionals
• Catching exceptions using try and except
• Short-circuit evaluation of logical expressions
Ex: x >= 2 and (x/y) > 2
52. Chapter 4 Functions
• Function Calls
• Built-in functions
• Type conversion functions
• Random numbers ---- Non Deterministic
• Math functions
• Adding new functions
• Definitions and uses
• Flow of execution
• Parameters and arguments
• Fruitful functions and void functions
• Why functions?
58. def print_lyrics(): print("I'm a lumberjack,
and I'm okay.") print('I sleep all night and I
work all day.')def repeat_lyrics():
print_lyrics() print_lyrics()repeat_lyrics()