ALGORITHMIC THINKING
WITH PYTHON
Essentials of Python Programming
Essentials of Python Programming
Python has the following features:
Python has simple, conventional syntax.
It is highly interactive: expressions can be entered at an
interpreter’s terminal to allow the programmer to try out the
code and receive immediate output.
It is object-oriented: OOP (Object Oriented Programming)
models real-world objects and their interactions, leading to
more natural and understandable program structures.
Python scales well: apt language for new programmers and
also cater to the research community’s needs.
It is interpreted: it is processed at runtime by the interpreter. It
need not be compiled before executing it.
Character set
Character set includes all valid characters that can be used in
Python programs. They are categorized as follows:
1. Alphabets:
a. Uppercase letters: A-Z
b. Lowercase letters: a-z
2. Digits: 0-9
3. Special characters: Symbols such as @, #, &, (, ), etc.
Python building blocks
1. Variables
2. Keywords
3. Literals
1. Variables
A variable associates a name with a value making it easy to
refer to and use that value later in a program.
Rules:
o A variable name must start with an alphabetic letter
o It can contain both letters and digits
o The underscore (_) is allowed
o Keywords cannot be used as identifiers
o Uppercase and lower case letters are distinct (sum and Sum
are different variables)
o Whitespace or blank spaces are not allowed
2. Keywords
Keywords are reserved words that have a predefined meaning
in a programming language.
Python has 29 keywords
1) and 11) exec 21) not
2) assert 12) finally 22) or
3) break 13) for 23) pass
4) class 14) from 24) print
5) continue 15) global 25) raise
6) def 16) if 26) return
7) del 17) import 27) try
8) elif 18) in 28) while
9) else 19) is 29) yield
10) except 20) lambda
3. Literals
A literal is a constant value that is directly assigned to a
variable
Examples of literals:
o Integer literals: a=5, b=10
o Floating-point literals: pi=3.14
o String literals: a=‘aron’, b=“manu’s”
Data Types
Numeric
Integer: Represents whole numbers, both positive and
negative, without any fractional component.
• Python integers can be of arbitrary size
• Eg: 5, -3, 1024
Float: Represents real numbers, or numbers with a fractional
component, stored as floating point numbers
• Eg: 3.14, -0.001, 2.718
Complex Number: Represents complex numbers with a real
and an imaginary part.
Dictionary
A dictionary is an unordered collection of key-value pairs.
Each key is unique and values can be accessed by their keys
Eg:
my_dict={‘name’:’ Alice’, ‘age’: 25, ‘city’: ’New York’}
print(my_dict[‘name’])
#Output : Alice
Boolean
It represents one of two values: ‘True’ or ‘False’
Used for logical operations and comparisons
Eg:
is_sunny = True
is_raining = False
print(is_sunny)
#Output : True
SET
A set is an unordered collection of unique elements.
Sets are useful for storing multiple items without duplicates
and performing set operations.
Eg:
my_set = {1, 2, 3, 4, 5}
my_set.add(6)
Print(my_set)
#Output: {1, 2, 3, 4, 5, 6}
Sequence Type
Strings :
A string is a sequence of characters enclosed within single,
double, or triple quotes.
Strings are immutable in Python.
Example:
greeting = “Hello, World!”
print(greeting)
#Output: Hello, World!
Sequence Type
List :
A List is an ordered and mutable collection of items, which
can contain elements of different data types.
Example :
my_list = [1, 2, ‘apple’, 4, 5]
my_list.append(‘banana’) //append means to add
print(my_list)
#Output : [1, 2, ‘apple’, 4, 5, ‘banana’]
Sequence Type
Tuple :
A tuple is an ordered and immutable collection of items.
Once created, it cannot be modified.
Example :
my_tuple = (1, 2, 3, ‘a’, ‘b’)
print(my_tuple)
# Output : (1, 2, 3, ‘a’, ‘b’)