Python
Python
INTRODUCTION
• Examples of programming
language are Python, Java, C, C+
+, etc.
INTRODUCTION
• Examples of programming
language are Python, Java, C, C+
+, etc.
INTRODUCTION
• Python is a high-level programming language.
• Python is:
1. Easy to read
2. Beginner friendly
3. Versatile
4. Portable
5. Community Support
• Python was created by Guido van Rossum, it
was released in 1991.
INTRODUCTION
Python works in two modes:
1. Interactive mode - Interactive mode allows you to execute Python code
line by line, immediately seeing the results.
2. Script Mode - Script mode involves writing Python code in script files
(with a .py extension) and executing them as a whole. This mode is suitable
for writing larger programs.
Variables
• In programming, a variable is a symbolic name that represents a
value stored in the computer's memory.
• Variable rules for Python:
1. Valid Characters
2. Case-Sensitivity
3. Reserved Keywords
4. No Spaces
• It's a good practice to choose descriptive and meaningful variable
names that indicate the purpose or content of the variable. This
makes your code more readable and understandable.
Variables
Valid variable names Invalid variable names
Arithmetic operators Addition (+), multiplication (*), Subtraction (-), division (/),
modulus (*), floor division(//), exponential (**)
Relational operators Equal to (==), not equal to (!=), less than(<), greater
than (>), less than equal to (<=), greater than equal to
(>=)
Logical Operators AND (and), OR (or), NOT (not)
Comments in Python
In Python, comments are used to annotate code with explanatory notes. Comments
are ignored by the Python interpreter and do not affect the execution of the program.
There are two types of comments:
1. Single line - Single-line comments begin with the # character and continue until
the end of the line.
Syntax - #This is a single line comment
2. Multi-line comment - For longer comments or documentation strings, you can use
multi-line strings enclosed in triple quotes.
Syntax – “““This is
multi-line
comment”””
Types of control structures
Control structures are used to control the flow of execution of a program
Types of control structures are:
1. Sequential statements - statements that are executed in sequence, one
after the other, in the order in which they appear in the code.
2. Conditional statements - allows you to execute different blocks of code
based on whether a certain condition is true or false.
3. Iterative statements – also known as loops, these statements allow you
to repeat a block of code multiple times.
Conditional Statements
if statement if..else statement if..elif..else
The if statement in Python is a The if..else statement in Python The if..elif..else statement allows
conditional statement that allows is an extension of the if you to specify multiple conditions
you to execute a block of code statement that allows you to and execute different blocks of
only if a specified condition is specify an alternative block of code based on the outcome of
true. code to execute when the those conditions. It provides a
The if statement in Python is a conditional statement that allows you to execute a block of code only if a specified condition
Syntax condition in the if statement way to make decisions with
is true.
if <condition>: evaluates to false. multiple possible outcomes.
statements Syntax Syntax
if <condition>: if <condition1>:
statements statements
else: elif <condition2>:
statements statements
else:
statements
Iterative statements
• Iterative statements, also known as loops, are programming constructs
used to repeat a block of code multiple times.
• Iterative statements reduces the lines of code in a program.
• There are 3 important components of an iterative statement:
1. a start value
2. a condition
3. an increment or decrement statement
• Every loop works with the help of a variable, the control variable
Iterative statements
• Iterative statements, also known as loops, are programming constructs
used to repeat a block of code multiple times.
• Iterative statements reduces the lines of code in a program.
• There are 3 important components of an iterative statement:
1. a start value
2. a condition
3. an increment or decrement statement
• Every loop works with the help of a variable, the control variable
Iterative statements
start value
increment or decrement statement
condition
Iterative Statements
for loop while loop