Professional Documents
Culture Documents
The if statement
• The simple if statement allows the program to branch based on the
evaluation of an expression
• The basic format of the if statement is:
The while loop
• A “while-loop” allows you to repeat a block of code until a
condition is no longer true:
while <condition>:
block of code
For loop
• The for loop, like the while loop repeatedly executes a set of
statements. The difference is that in the for loop we know in at
the outset how often the statements in the loop will be
executed.
• The basic format of the for statement is
for variable_name in some_sequence :
statement1
statement2
...
statementn
Exercise
1. Write a program in python to check whether the entered year is a
leap year or not.
2. Write a program in python to check the person age and accordingly
show him the price of train ticket that fit his age.
• #children until 18 years ticket = 20
• #old people bigger than or equal 60 years ticket = 50
• #young people from 18 - 59 year ticket = 100
3. Write a program to display numbers from 1 to 10
4. Write a program to find the number of character ‘a’ in the letter
‘banana’.(change the indentation and check the answer)
How to read data from the user
• To prompt the user to enter the values we make use of input()
method.
• For Example: Write a program to enter a name and age, converts the
age into an integer, and then displays the data.
• The function definition opens with the keyword def followed by the
name of the function and a list of parameter names in parentheses.
The body of the function — the statements that are executed when it
runs — is indented below the definition line.
def celsius_to_fahr(temp):
return 9/5 * temp + 32
freezing_point = celsius_to_fahr(0)
print(freezing_point)
remainder(5, 3)
Output: 2
remainder(5, divisor=3)
Output: 2
Ans: ln:29
Example 3:
• Create a new function called hello with 2 parameters
• Parameter 1 should be called name and you should assign some text to this
parameter this when using the function
• Parameter 2 should be called age and you should provide a number value for this
parameter when using the function
• When using the function, the value that is returned should be a character
string stating the name and age that were provided, which you can assign
to a variable called output. Printing out output should produce something
like the following:
print(output)
'Hello, my name is Dave. I am 39 years old.'
Anonymous Functions – Lambda
• A lambda function is a small function containing a single
expression.
• Lambda functions can also act as anonymous functions where
they don’t require any name.
• These are very helpful when we have to perform small tasks
with less code.
• We can also use lambda functions when we have to pass a small
function to another function.
• The lambda's general form is :
lambda arg1, arg2, ...argN : expression using arguments
Cont…
• We can also use lambda functions when we have to pass a small
function to another function.
• In Python, lambda functions have the following syntax:
• Lambda functions consist of three parts:
• Keyword
• Bound variable/argument, and
• Body or expression
• The keyword is mandatory, and it must be a lambda, whereas
the arguments and body can change based on the requirements.
Comparing Lambda with Regular function
Lambda functions are defined using the keyword lambda. They can have
any number of arguments but only one expression. A lambda function
cannot contain any statements, and it returns a function object which
can be assigned to any variable. They are generally used for one-line
expressions.
Regular functions are created using the def keyword. They can have any
number of arguments and any number of expressions. They can contain
any statements and are generally used for large blocks of code.
Example
• (lambda x: x*x*x)(10)
• f = lambda x, y, z: x + y + z
f(2, 30, 400)
import time
seconds = time.time()
print("Seconds since epoch =", seconds)
• Python time.ctime()
The time.ctime() function takes seconds passed since epoch as an
argument and returns a string representing local time.
import time
# seconds passed since epoch
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)
Cont…
• Python time.sleep()
The sleep() function suspends (delays) execution of the current
thread for the given number of seconds.
import time
print("This is printed immediately.")
time.sleep(2.4)
print("This is printed after 2.4 seconds.")
Cont…
• Python time.strftime()
• The strftime() function takes struct_time (or tuple corresponding to it) as an
argument and returns a string representing it based on the format code used.
• Here, %Y, %m, %d, %H etc. are format codes.