COURSE: PYTHON PROGRAMMING FUNDAMENTALS
TOPIC: INTRODUCTION, SYNTAX, AND CONTROL FLOW
SEMESTER NOTES: UNIT 1
1. INTRODUCTION TO PYTHON
Python is a high-level, interpreted, general-purpose programming language. Created by Guido
van Rossum and first released in 1991, Python's design philosophy emphasizes code
readability with its notable use of significant indentation.
- Key Features:
- Interpreted: Python is processed at runtime by the interpreter. You do not need to compile
your program before executing it. This is similar to PERL and PHP.
- Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to
write your programs.
- Object-Oriented: Python supports Object-Oriented style or technique of programming that
encapsulates code within objects.
- Beginner's Language: Python is a great language for the beginner-level programmers and
supports the development of a wide range of applications.
2. PYTHON SYNTAX AND VARIABLES
- Indentation: Most languages (C, C++, Java) use braces { } to define a block of code. Python
uses indentation. A code block (body of a function, loop, etc.) starts with indentation and ends
with the first unindented line.
- Comments: Single-line comments start with a hash sign (#). Multi-line strings can be used as
comments (docstrings) using triple quotes.
- Variables: Python variables do not need explicit declaration to reserve memory space. The
declaration happens automatically when you assign a value to a variable.
x = 10 # Integer
name = "John" # String
pi = 3.14 # Float
3. DATA TYPES
Python has various standard data types that are used to define the operations possible on them
and the storage method for each of them.
- Numbers: int, float, complex.
- String: Contiguous set of characters represented in the quotation marks. Python allows for
either pairs of single or double quotes.
- List: The most versatile compound data type. A list contains items separated by commas and
enclosed within square brackets [].
- Tuple: Similar to lists, but immutable (read-only). Enclosed in parentheses ().
- Dictionary: Hash table type. Works like an associative array or hash found in Perl and consists
of key-value pairs.
4. OPERATORS
- Arithmetic: +, -, *, /, %, **, // (Floor Division).
- Comparison: ==, !=, >, <, >=, <=.
- Logical: and, or, not.
- Membership: 'in', 'not in'. (e.g., if 'a' in list_of_chars).
- Identity: 'is', 'is not'. (Checks if two variables point to the same object in memory).
5. CONTROL FLOW: CONDITIONAL STATEMENTS
Decision-making is anticipation of conditions occurring while execution of the program and
specifying actions taken according to the conditions.
- if statement:
if expression:
statement(s)
- if...else statement:
if expression:
statement(s)
else:
statement(s)
- if...elif...else statement:
if expression1:
statement(s)
elif expression2:
statement(s)
else:
statement(s)
6. CONTROL FLOW: LOOPS
In general, statements are executed sequentially. A loop statement allows us to execute a
statement or group of statements multiple times.
- While Loop: Repeats a statement or group of statements while a given condition is TRUE. It
tests the condition before executing the loop body.
count = 0
while (count < 9):
print('The count is:', count)
count = count + 1
- For Loop: The for loop in Python has the ability to iterate over the items of any sequence, such
as a list or a string.
fruits = ['banana', 'apple', 'mango']
for fruit in fruits:
print('Current fruit :', fruit)
- Range Function: To iterate over a sequence of numbers, the built-in function range() is used.
for i in range(5): # Generates 0, 1, 2, 3, 4
7. LOOP CONTROL STATEMENTS
- Break: Terminates the loop statement and transfers execution to the statement immediately
following the loop.
- Continue: Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
- Pass: The pass statement is a null operation; nothing happens when it executes. It is useful as
a placeholder when a statement is required syntactically, but no code needs to be executed.
8. CONCLUSION
Mastery of control flow is the first step in programming logic. Python's clean syntax allows
developers to focus on the logic rather than the boilerplate code required by languages like
Java.