Python Lab 2
Python Lab 2
Python Comments
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing
code.
Example
#This is a comment
print("Hello, World!")
Multiline Comments
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python
C++
Output
The all() method returns True when all elements in the given
iterable are true. If not, it returns False.
Syntax
all(iterable)
Parameters
# empty iterable
l = []
print(all(l))
Python bin() Function:
Definition
The bin() method converts and returns the binary equivalent string of a given
integer.
Syntax
The syntax of bin() method is:
bin(num)
Parameters
he bin() method takes a single parameter:
num – an integer number whose binary equivalent is to be calculated.
Example:
number = 5
print('The binary equivalent of 5 is:', bin(number))
Output
The binary equivalent of 5 is: 0b101
We will now see how to define and use a function in a Python program.
Defining a Function
A function is a reusable block of programming statements
designed to perform a certain task. To define a function, Python
provides the def keyword. The following is the syntax of defining
a function.
Syntax:
def function_name(parameters):
"""docstring"""
statement1
statement2
...
...
return [expr]
User-defined Function
def greet():
print("sadaf")
Calling User-defined Function
greet()
Output
Hello World!
By default, all the functions return None if the return statement does not exist.
def greet(name):
print ('Hello ', name)
Output
Hello Steve, Bill, and Yash
The following function works with any number of arguments.
def greet(*names):
i=0
print('Hello ', end='')
while len(names) > i:
print(names[i], end=', ')
i+=1
Output:
Hello Steve, Bill, Yash, Hello Steve, Bill, Yash, Kapil,
John, Amir,
Function with Keyword Arguments
def greet(firstname, lastname):
print ('Hello', firstname, lastname)
Output:
Hello Steve Jobs
Function with Return Value
def sum(a, b):
return a + b
total=sum(10, 20)
print(total)
total=sum(5, sum(10, 20))
print(total)
Output
30
35
Operator Description
() Parentheses
** Exponentiation (raise to the power)
~+- Complement, unary plus and minus
* / % // Multiply, divide, modulo, and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise ‘AND’
Bitwise exclusive ‘OR’ and regular ‘OR’
^|
• if statement
• if...else statement
• if...elif...else statement
1. Python if statement
The syntax of if statement in Python is:
if condition:
# body of if statement
The if statement evaluates condition.
if number > 0:
print('Positive number')
else:
print('Negative number')
Output
Positive number
3. Python if...elif...else Statement
The if...else statement is used to execute a block of code among two
alternatives.
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
Here,
If condition1 evaluates to true, code block 1 is executed.
If condition1 evaluates to false, then condition2 is evaluated.
If condition2 is true, code block 2 is executed.
If condition2 is false, code block 3 is executed.
Example 3: Python if...elif...else Statement
number = 0
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
Output:
Zero
Python Nested if statements
We can also use an if statement inside of an if statement.
This is known as a nested if statement.
# outer if statement
if condition1:
# statement(s)
# inner if statement
if condition2:
# statement(s)
Notes:
# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')