Python developed by Guido Van Rossum
Features
● high level language.
● free and open source language.
● an interpreted language, as Python programs are executed by an interpreter.
● is case-sensitive.
● portable and platform independent, meaning it can run on various operating systems and
hardware platforms.
● has a rich library of predefined functions.
Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode-allows execution of individual statements immediately.
● Working in the interactive mode is convenient for testing a single line code for instant
execution.
● But in the interactive mode, we cannot save the statements for future use and we have
to retype the statements to run them again.
b) Script mode-allows us to write more than one instruction in a file called Python source code
file that can be executed.
● Working in the script mode is convenient for testing multiple lines of code.
● In script mode, we can save the statements for future use.
Tokens
Basic building blocks of a programming language KILPO
a)Keywords-reserved words that convey a special meaning to the Python interpreter.
b)Identifiers(variables)- identifiers are names used to identify a variable, function, or other entities in
a program.
Rules
1. should begin with an uppercase or a lowercase alphabet or an underscore sign (_).
2. an identifier cannot start with a digit.
3. can be of any length. (However, it is preferred to keep it short and meaningful).
4. should not be a keyword or reserved word
5. cannot use special symbols like !, @, #, $, %, etc., in identifiers.
c)Literals-values given to a variable.
String, boolean, integer, floating point, character, special literal None
d)Punctuators-special characters used ;:,”{}’[ ]()
e)Operators
● Arithmetic + - * / % // **
● Assignment = += -= *= /= %= //= **=
● Logical Not,and,or
● Relational > >= < <= == !=
● Membership in, not in
● Identity is, is not
Expressions
a combination of constants, variables, and operators.
Eg: c=a+b
Datatypes-identifies the type of data values a variable can hold and the operations that can be
performed on that data.
Comments
● used to add a remark or a note in the source code.
● not executed by interpreter.
Two types
● Single line comment using #
● Multiline using “ “ “ or ‘ ‘ ‘
Control statements
1. Conditional statements
a. Simple if-used for 1 condition
Syntax:
if condition:
statement(s)
b. if…else-used for 2 conditions
Syntax:
if condition:
statement(s)
else:
statement(s)
c. if elif ladder-used for more than 2 conditions
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
d. Nested if- if inside if
Syntax:
if <condition-1>:
if <condition - 1.1>:
<statement>
else:
<statement>
else:
<statement>
2. Looping/ iterational statements
a. for-repeats a set of statements until it reaches a certain limit
Syntax:
for <control-variable> in <sequence/items in range>:
<statements inside body of the loop>
b. while
Syntax:
Initialisation
while <test_condition>:
body of while
updation
c. Nested loop-loop inside loop
3. Jump statements
a. break-used to terminate the current loop completely. When a break
statement is encountered inside a loop, the loop is immediately exited, and
the program execution continues with the statement immediately following
the loop.
Eg:
for i in range(10):
if i==4:
break
print(i)
Prints all values from 0 to 3
b. continue-used to skip the rest of the current iteration of the loop and move
on to the next one. When a continue statement is encountered, the
remaining code inside the current loop's body for that specific iteration is
skipped, and the loop proceeds to its next iteration.
Eg:
for i in range(10):
if i==4:
continue
print(i)
Prints all values from 0 to 9 except 4
c. pass-empty statement which does nothing
Eg:
if 5 > 2:
pass
else:
print("This will not be printed.")
d. return-used to send a result back to the part of the code that called the
function.
Eg:
def calculate_sum(numbers):
total = 0
total += num
return total
Statements - a statement is a unit of code that the Python interpreter can execute.
2 types.
Input statements
input()-used to take input from the user
Eg: a=int(input(“Enter a value”))
Output statements
print()-give output to the user
Eg: print(“Hello”)