Unit I PPS
Unit I PPS
4 Strings in Python
Unit One
• Problem Solving, Programming and Python Programming
• Contents:
1. General Problem Solving
2. Program Design Tools
3. Basics of Python Programming
Problem and Problem Solving
• Problem:
A problem exists for an individual when he has a definite goal he can not reach.
• Problem Solving:
It is a cognitive processing directed at achieving a goal where no solution method
is obvious to the problem solver.
High-Level
Large
Broader
General
Low-Level
Small
Specific
Detailed
Top Down Design Approach
• Top Down Approach for Dividing Problem into sub problem: Top-Down Approach
Problem
Problem A Problem B
+ *
Top Down Design Approach
• Top Down Approach for Getting Information at different Levels: Top-Down Approach
Top Down Design Approach
• Example 2:
Program Design Tools
• Program:
Program is a set or collection of Instructions.
• Programming Language:
Programming Language is a way for communicating with computer through
programs.
Through programming language we can describe designing tools.
Example: Python, Java, C, C++, .Net, R, HTML, Javascript and so on.
• Characteristics of Algorithm:
• Minimum one Input
• Minimum one Output
• Finiteness (Finite number of Instructions )
• Effectiveness (Algorithms should be most effective among many
different ways to solve a problem)
• Definiteness (Each step in the algorithm should be clear and unambiguous.)
• Example 1: Algorithm for Making Tea Program Design Tools
Step 1: Start
Step 9: Stop
Program Design Tools
• Example 2: Algorithm for Adding Two Integer Numbers
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
Step 6: Stop
• Example 3: Algorithm for Even/Odd Number Program Design Tools
Step 1: Start
Step 7: Stop
Program Design Tools
• Flowchart: Flowchart is the pictorial presentation of algorithm.
Program Design Tools
• Example 2: Algorithm for Adding Two
Integer Numbers
Step 1: Start
Step 6: Stop
Program Design Tools
• Example 3: Algorithm for Even/Odd Number
Step 1: Start
Step 7: Stop
Programming Language
• Definition:
A programming language is a notation designed to connect instructions to a
machine or a computer.
Programming languages are mainly used to control the performance of a
machine or to express algorithms.
• Program:
A computer program is a collection of instruction that can be executed by
a computer to perform a specific task
Types of Programming Language
High-Level Language
Interactive Mode
• It should not start with number and symbol (except underscore “_”).
Example: 1number, 45name, #division, *pincode are not valid
Example: number1, name45 are valid
Value Stored in
Memory the Memory
Cell
Variable Name
Memory Cell
Variable in Python
Value Stored in
the Memory 10 25.50 AVCOE
Cell
Variable Name a b c
Assigning values to Variables in Python
Declaring and assigning single value to a variable
a = 10 -----> Declared variable a and assigned value 10.
a, b, c = “AVCOE”
Declared variable a, b and c and assigning same value AVCOE to all variables.
Assigning values to Variables in Python
Changing value of a variable in same program
Program 1 Output 1
a = 10
10
print(a)
Program 2 Output 2
a = 10
10
print(a)
AVCOE
a = “AVCOE”
print(a)
Constant in Python
Definition:
A constant is a type of variable whose value cannot be changed.
PI = 3.14
• Numeric Literals
• String literals
• Boolean literals
• Special literals
• Literal Collections
Numeric Literals in Python
Definition: Numeric Literals are immutable (unchangeable). Numeric literals can
belong to 3 different numerical types: Integer, Float and Complex
Binary Literals:
Binary literals begin with a leading 0b or 0B, followed by binary digits (0-1).
a = 0b1100 -------> 12, a = 0B1111 ------> 15
Decimal Literals:
Decimal literals are specified as signed or unsigned numbers of 1 to 38.
a = 100 -------> 100, a = 200 ------> 200
Numeric Literals in Python
Definition: Numeric Literals are immutable (unchangeable). Numeric literals can
belong to 3 different numerical types: Integer, Float and Complex
Octal Literals:
Octal literals start with a leading 0o or 0O (zero and lower- or uppercase letter o),
followed by a string of digits (0-7).
a = 0o14 -------> 12, a = 0O24 ------> 20
Hexadecimal Literals:
Hexadecimals start with a leading 0x or 0X, followed by a string of hexadecimal
digits (0-9 and A-F).
a = 0x11 -------> 17, a = 0X1a ------> 26
String Literals in Python
Definition: A string literal is a sequence of characters surrounded by quotes. We
can use both single, double, or triple quotes for a string. And, a character literal is a
single character surrounded by single or double quotes.
String Literals:
Program 1 Output 1
a = None
print(a)
None
Literal Collections in Python
Definition: There are four different literal collections
1. List
2. Tuple
3. Dictionary
4. Set
5. String
1. Arithmetic Operators
2. Relational/Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Identity Operators
7. Membership Operators
Arithmetic Operators
1. Addition (+)
2. Substraction (-)
3. Multiplication (*)
4. Division (/, forward slash)
5. Floor division (//, two forward slash)
6. Power/Exponential Operator (**)
7. Modulus or Mod Operator (%)
a=7 a=6
b = a<<1 c = a >>1
print(b) print(c)
14 ?
a=7 a=7
b = a<<2 c = a>>3
print(b) print(c)
28 ?
Assignment Operators
• Assignment operators are used in Python to assign values to variables.
Identity Operators:
Remember:
• Identity operators check the memory location of the variable and not contents.
• If two variables have same values that doesn’t mean that they are identical.
a, b a b
a = 10 a = 10
b = 10 10 b = 20 10 20
FFF0001 FFF0001 FFF0002
Special Operators in Python
Membership Operators:
• A code block start with indentation and ends with the first unindented line.
as elif if or yield