Getting Started With Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Class XI Informatics Practices

UNIT- 2 CHAPTER 2

GETTING STARTED WITH


PYTHON
Python was conceived in the late 1980s by
Guido van Rossum at Centrum Wiskunde & Informatica (CWI)
in the Netherlands as a successor to the ABC language
Why Python

• Productivity and Speed of Development


• Learning Ease and Support Available
• Extensive Support Libraries
• User-friendly Data Structures
• Object Oriented Language
• Free and Open Source
It is used for..

• Software development
• Web development (server-side)
• System scripting
• Mathematics
Features of Python
1. Easy to use – Due to simple syntax rule
2. Interpreted language – Code execution & interpretation line by line
3. Cross-platform language – It can run on windows,linux,macintosh etc. equally
4. Expressive language – Less code to be written as it itself express the purpose of
the code.
5. Completeness – Support wide rage of library
6. Free & Open Source – Can be downloaded freely and source code can be
modify for improvement
Limitations of Python
1. Lesser libraries – as compared to other programming languages like
c++,java,.net
2. Slow language – as it is interpreted languages, it executes the program
slowly.
3. Weak on Type-binding – It not pin point on use of a single variable for
different data type.
Let’s Start Exploring 😊

https://www.python.org/downloads/

1. Click Download
2. Run the file
3. Select Add Python to PATH
4. Click Install Now
5. Next
6. Next
7. And you are Done
Don’t Forget to Select This
Programming Environment
• The default graphical development environment for Python is IDLE.
(Integrated Development and Learning Environment)
• IDLE comes with the Python installation.
• pip is the preferred installer program which is also included by default.
• PyCharm, PyDev, Spyder are other popular IDE’S
(Integrated development environment)
INTERACTIVE MODE
(PYTHON SHELL)

PYTHON

SCRIPT MODE
(PYTHON EDITOR)
Interactive Mode
Click start button -> All programs -> python <version> -> IDLE
Click start button -> All programs -> python <version> -> Python.exe
Interactive Mode
• The interactive mode involves running your codes directly on
the Python shell (Interpreter)
• As soon as we press enter, the interpreter executes the statement and displays
the result
• Interactive mode is handy when you just want to execute basic Python
commands (One command at a time, Immediate Output)
• we cannot save the statements for future use
Python Shell

The symbol >>> is called


Python prompt

The >>> indicates that


the Python shell is ready
to execute command
Script Mode
• In the script mode, we can write a Python program in a file, save it and then
use the interpreter to execute the program from the file.
• Such program files have a .py extension and they are also known as scripts
Python Editor
• In Python IDLE
Click File -> New
• You will get a editor window
• Type you commands
• To Save
File -> Save
• Execute Program
Run -> Run Module (F5)
Interactive v/s Script
Python Character Set
A set of valid characters recognized by python. Python uses the traditional ASCII
character set. The latest version recognizes the Unicode character set. The ASCII
character set is a subset of the Unicode character set.
• Letters :– A-Z,a-z
• Digits :– 0-9
• Special symbols :– Special symbol available over keyboard
• White spaces:– blank space,tab,carriage return,new line, form feed
• Other characters:- Unicode
Python Tokens
In a passage of text individual words and punctuation marks are called tokens or
lexical units. Smallest individual unit in a program is also known as token.
The following are python tokens
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators
1. Keywords
• Keywords are reserved words. Each keyword has a specific meaning to the
Python interpreter. As Python is case sensitive, keywords must be written
exactly as given below
2. Identifiers
In programming languages, identifiers are names used to identify a variable, function,
or other entities in a program. The rules for naming an identifier in Python are as
follows:
 The name should begin with an uppercase or a lowercase alphabet or an underscore
sign (_). Thus, an identifier cannot start with a digit.
 It can be of any length. (However, it is preferred to keep it short and meaningful).
 It should not be a keyword or reserved word.
 We cannot use special symbols like !, @, #, $, %, etc. in identifiers.
Let’s Check
• myVariable • My India
• 007bond • RollNo.
• variable_1 • 14_Mark
• T@ta • False
• true • TotalSum
• Heavy14Air • GRADEPAY
Variables
Variable is a name given to a memory location. A variable refers to some location in
the memory which contains value. Python is a type infer language that means you don't
need to specify the datatype of variable. Python automatically get variable datatype
depending upon the value assigned to the variable.
x= “Text”
y = 78
z = 6.2
sum = None
Multiple Assignment
• Assigning the same value to multiple variable
a=b=c=10 a=10
b=10
c=10

• Assigning multiple values to multiple variables


a,b,c=10,20,30 a=20
b=20
c=30
3. Literals
Literals in Python can be defined as number, text, or other data that represent
values to be stored in variables. Literals have fixed value.(Constants)
• String Literal- sequence of characters enclosed in quotes
• Numeric Literal- int, float, complex
• Boolean Literal- True, False
• Special Literal- None
 Example of String Literals in Python
name = ‘Johni’, fname =“johny”
 Example of Integer Literals in Python(numeric literal)
age = 22
 Example of Float Literals in Python(numeric literal)
height = 6.2
 Example of Boolean Literals in Python
Choice= True, Pass= False

 Example of Special Literals in Python Python has one special literal called 'None'. The
name = None 'None' literal is used to indicate something that
has not yet been created
String Literals
 Single Line Strings
Strings we create using single or double quotes are normally single line.
They terminate in one single line.
Subject=“python”
Subject=“python

EOL Error: By default python creates strings with both quotes and it must terminate
in the same line by quotes
String Literals
 Multi Line Strings
We use multiline strings in python to store some text across multiple lines.
Two ways – Escape character as EOL or triple quotes
Subject=“python\
is interesting“ Subject=“””python
is interesting”””
print(Subject) print(Subject)
OUTPUT OUTPUT
python is interesting python
is interesting
String Literals
• Size of string = No. of characters in it
• Escape sequence is counted as one character
Floating Literals
• Fractional Form: signed or unsigned with decimal part
• E.g. 14.1, -121.0, 0.7
• Exponent Form: it consist of two parts “Mantissa” and “Exponent”
E.g. 25.5 = 0.255 * 102
= 0.255E02

Mantissa=0.255, Exponent=02
Escape Sequences

 Non-graphic characters in string


values
 We can not directly type it from
keyboard
 In Python strings, the backslash "\" is
a special character, also called the
"escape" character
 Escape Sequence is backslash
followed by one or more characters
4. Operators
An operator is used to perform specific mathematical or logical operation on
values. The values that the operator works on are called operands
• Arithmetic Operators
• Relational Operators
• Assignment Operators
• Logical Operators
• Membership Operators
• Identity Operators
Arithmetic Operators
Concatenation & Replication
• Python supports arithmetic operators to perform the four basic arithmetic operations as well as
modular division, floor division and exponentiation.
• '+' operator can also be used to concatenate two strings on either side of the operator.
> > > str1 = "Hello“
>>> str2 = "India"
> > > str1 + str2 'HelloIndia'
• '*' operator repeats the item on left side of the operator if first operand is a string and second
operand is an integer value.
>>> str1 = 'India'
> > > str1 * 2 'IndiaIndia'
Arithmetic Operators
Relational Operators
• Relational operator compares the values of the operands on its either side
and determines the relationship among them
Relational Operators

• Similarly, there are other relational operators like < = and > =
Assignment Operator
• Similarly, there are other assignment operators like *=, /=, %=, / / = , and
**=.
• Known as Augmented Assignment Operators / Short Hands
Logical Operators
Membership Operators
The membership operators in Python are used to validate whether a value is
found within a sequence such as such as strings, lists, or tuples
Eg: Str=“test”
print(“t” in Str)
Membership Operators
Identity Operator

is operator
Returns true if both variables point to
the same object and false otherwise
Eg: X is Y
is not operator
Returns false if both variables point to
the same object and true otherwise
Eg: X is not Y
Identity Operator v/s = = Operator
• Identity operator ( “is” and “is not” ) is used to compare the object's
memory location. '==' compares if both the object values are
identical or not
X= 10 145465654 133365654
Y= 10
Z 10 10
Z= X
print(X==Y) True X
print(X is Y) False Y
print(X==Z) True
print(X is Z) True
Operator Precedence

Precedence is used
to decide ,which
operator to be
taken first for
evaluation when
two or more
operators comes
in an expression.
5. Punctuators

• Used to implement the grammatical and structure of a Syntax.


• Works as separators between other elements
• Organize sentence structure and program structure
• Following are the python punctuators.
( ) { } [ ] ;, . \ # @:= ‘“

You might also like