After the end of lesson you will be able to learn Python basics-What Python is? Its releases. Where we can use Python? Python Features. Tokens, comments variables etc... In out next PPT you will learn how to input and get output in Python
1. Content
1. What is Python
2. Python Release
3. Application of Python
4. Features of Python
5. Tokens in Python
6. Comments in Python
7. Variables
2. 1. What is Python?
Python is an interpreted, object-oriented, high-level
programming language for general-purpose(use for multi purpose
e.g. web app, desktop app, data science etc) programming. Created by
Guido van Rossum and first released in 1991.
Python is a cross-platform programming language,
meaning, it runs on multiple platforms like Windows, Mac
OS X, Linux, Unix.
Rossum was fan of a comedy series from late seventies.
The name "Python" was adopted from the same series
"Monty Python's Flying Circus".
3. 2. Major release date of Python
Version Release date
Python 1.0 (Version) January 1994
Python 1.6 5 Sept 2000
Python 2.0 16 Oct 2000
Python 2.7 4 Jul 2010
Python 3.0 3 Dec 2008
Python 3.6.0 23 Dec 2016
Python 3.6.5 (Latest) 28 Mar 2018
4. 3. Application of Python
Where we can use Python
Desktop Application
Web and Internet Application
Database Application
Networking Application
Games
Data Analysis (data science)
Machine Learning
Artificial Application
5. 4. Features Of Python
Simple and easy to learn
It's much easier to read and write Python
programs compared to other languages like: C++,
Java, C#.
Free and Open source
You can freely use and distribute Python, even for
commercial use.
Also you can able to see the source code and
even can make changes to the code.
6. Cont..
High level programming language
We are never required to worry about low level
activities like memory management, securities etc.
Programming friendly language
Platform Independent
• On any machine like widows, Linux or Mac we
are able to run the code anywhere
• We write code only once and can run on different
machine
• Write once and run anywhere (WORA)
7. Cont....
Portability
You can move Python programs from one
platform to another, and run it without any
changes.
Python
Program
written in
windows
Linux, MAC
etc
Portable
Think it as a mobile number portability, Airtel can be transferred to idea
operator without changing the phone number
8. Cont...
Dynamically typed
In python, there is no need to declare data type for
the variables, it internally create types, referring to the
values.
C, C++ or Java are statically typed
x = 10, python automatically assigns x as int
x = 10.5, python now accept it as float
x = True (now, type = Boolean)
so the variables are dynamically typed. In C or C++ or even in java if you
declare x as int then you cannot use it for a float value, if you do they
will through error
9. Cont....
Object-Oriented
Python is object oriented as it supports
Class, Object, Data hiding, Data Encapsulated,
Polymorphism etc. It is also an procedural oriented as
we can make the program without classes if we don’t
need security.
C is procedural objected language but not
object-oriented
Java is Object-Oriented not procedural
because you can't code anything in Java without
declaring classes and objects.
10. Cont....
Interpreted Language
when you run Python code, it automatically
converts your code to the language your computer
understands.You don't need to worry about any
lower-level operations.
It scans code line by line.
Python is Extensible
You can easily combine pieces of C/C++ or other
languages with Python code.
We can improve the performance of the
application using this feature
11. 5. Tokens
Individual words and punctuation marks are
called tokens or lexical units.
Python has following tokens:
5.1. Keywords
5.2. Identifiers (Name)
5.3. Literals
5.4. Operators
5.5. Punctuators
12. 5.1. Keywords
Keywords are the reserved words and convey
special meaning. Has 33 keywords
False assert del for in or while
True break elif from is pass with
None class else global lambda raise yield
and continue except if nonlocal return
as finally import not def try
13. 5.2. Identifiers
An identifier are the names to different part of the program
e.g. Variables, objects, classes, functions, list, dictionaries etc.
Allowed symbols are (a to z), (A to Z), (0 to 9) and (_).
Should not start with digits. (can be start by a letter or
underscore(_)).
The digits ( 0 through 9) can be part of identifier except for
the first character
Case-sensitive.
Cannot use reserved words as identifiers.
Identifiers are unlimited in length (no length limit).
If any identifier starts with _ (e.g., _x) then this variable is
treated as private. If uses two (e.g., __x) then it is strongly
private
15. 5.3. Literals/Values
Literals often referred to as constant-values
that have a fixed value.
Python allows several kinds of literals:
5.3.a. String literals
5.3.b. Numeric literals
5.3.c. Boolean literals
5.3.d. Special literal None
16. 5.3.a. String literals
String literal can be form by enclosing
text in single or double quotes
E.g., ‘Arun’ ‘HelloWorld’ “112”
Python allows two string types:
✓ Single line string
✓ Multi line string
17. Single line string Multi line string
Terminate in one line
E.g.,Text = ‘Hello’
E.g.,Text = “Hello”
Text = ‘hello
world’
Python will show you an
error
Text spread across multiple
lines as one single string
Can be created in two ways
either by triple quotation
mark (‘’’) or by backslash()
E.g.,Text = ‘hello
world’
Text = ‘’’this
Is another
multiline string’’’
18. Cont...
Multiline string literals can be created by two ways
either by triple quote(‘’’) or by backslash()
But the difference
This is only for the sake of
better readability in the
program. It doesn’t gives the
new line in the output, usually
uses in shell.
This is used when you have to
print multiple lines in output.
Gives output same as you write.
19. 5.3.b. Numeric Literals
There are 3 numerical types
Int :- also called integers, are positive or negative whole
numbers with no decimal point (E.g., 2, -6). Int can be
represented in 4 ways and will be discuss in next slide.
Float :- represent real numbers and written with a
decimal point. (e.g., 17.5, -13.0)
Complex :- are of the form a + ib, where a and b are
floats and i =
One more numerical literal is Long which is used in 2.x but removed from
Python 3.
20. Cont...
int values further represented in 4 ways:
Decimal values(Base 10): sequence of digits (0 to 9)
unless start with zero(e.g., 12, -7).
Binary values(Base 2): sequence of only 1 and 0 , starts
with 0B or 0b (e.g., 0b1010).
Octal values(Base 8): sequence of digits(0 to 7) starts
with 0O or 0o(zero-O) (e.g., 0o241)
Hexadecimal values(Base 16):sequence of(0 to 9, a to
f,A to F) preceded by 0x or 0X(e.g., oxFace, oxBk9)
In Python 2.x, octal value can also be represented as preceded by only 0 or 0o
or 0O but preceding by only 0 will through an error in 3.x
21. 5.3.c. Boolean Literal
A Boolean literal can either have True or
False.
5.3.d. Special literal None
The None literal in python means
“There’s nothing”
22. 5.4. Operators
An Operator performs operation on data.
There are 7 operator in python
✓ Arithmetic operator
✓ Assignment operator
✓ Relational operator
✓ Logical operator
✓ Bitwise operator
✓ Membership operator
✓ Identity operator
We will learn more about operators in python in our next PPT
23. 5.5. Punctuators
Punctuators are symbol/character that has
syntactic and semantic meaning to the compiler,
they are used in programming language to
organize sentence structure.
Most common punctuators in Python are:
‘ “ # ( ) [ ] { } @ , : . = ; `
24. 6. Comments in Python
Comments are additional readable information, which is
read by the programmers but ignored by Python
interpreter.
A single line comment can be inserted with #.
A multi line comment can be inserted with triple-
apostrophe(‘’’) or triple quotes(“””)
‘’’Multiline comments are useful for detailed additional information related to
the program.This type of multiline comment also known as docstring.
25. 7. Variables
Variable is a named location used to store data
in the memory
OR
Reserved memory location to store values.
Each variable must have a unique name called
identifier. (discussed identifier in 5.2)
Creating variable
MyName = “sajjad’
26. Cont...
Variable is not created until some value is assigned to it.
We can use the del statement to remove a variable
#Variable b is not assigned
27. Cont...
In Python, variables do not need declaration to
reserve memory space this makes it dynamically
typed.
“Variable declaration“ happens automatically
when we assign a value to a variable.
Python create a variable of the type similar to
the type of value assigned.
E.g., age = 20
age is a numeric variable
28. Cont...
In Python, program access data values through
references.
A reference is a name that refers to the specific
location in memory of a value (object).
Python internally create labels referring to
these values
Name
Age
‘sajjad’
22
30. Cont....
Multiple Assignments
Assign same value to multiple variable
a = b = c = d = 10
Assign multiple values to multiple variable
p, q, r = 5, 10, 7.5
Expression separated with commas are evaluated
from left to right
E.g., x, x = 10, 12
print(x) #12
34. Cont...
DynamicType
For instance,
x = 10 #variable x is of int type
if you assign value of some other type to x, python will
not complain(no error)
x = 10
print(x)
x = ‘hello’ x
print(x)
Output will be:
10
hello
Int: 10
String: hello
X doesn’t have type but value it points to have type. So we can make variable
point to a value of different type.This is called dynamicTyping.