Python Programming Lectures.
Python Programming Lectures.
LECTURES.
Python:
Installation
https://www.python.org/downloads/windows/
Python:
www.anaconda.com
Python:
Variables are kind of placeholders or these are names or symbols that can hold different kind
of values in it. Python supports several kind of variable like integer, float, string….etc.
x=2
y=5
c= 7.2
a,b= 4, 5.0
When we assigned the value x we never specified that it is integer that’s what we call it as
dynamically typed. Based on the content the type is defined automatically.
Type()
Arithmetic Operator
Len()
Upper()
Lower()
Endswith()
Count()
Capitalize
Find()
Replace(oldword ,newword)
Scape Sequence;
\n
\\
\’
\t
…….E.t.c
Iteration:
For Loop
While Loop
A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once for each
item in a list, tuple, set etc.
• Looping Through a List, Tuple, Dictionary , Set, String
for x in range(6):
if x==4:
break
print(x)
for x in range(6):
if x==4:
continue
print(x)
Python For Loop:
for x in range(6):
print(x)
else:
print("Finally finished!")
Print all numbers from 0 to 5, and print a message when the loop has ended:
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
Note: The else block will NOT be executed if the loop is stopped by a break
statement.
Practice Question:
i=1
while i < 6:
print(i)
i += 1
Print i as long as i is less than 6:
Note: remember to increment i, or else the loop will continue forever.
The while loop requires relevant variables to be ready, in this example we need to define an
indexing variable, i, which we set to 1.
Python While Loop:
The break Statement:
With the break statement we can stop the loop even if the while condition is true:
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
Python While Loop:
The continue Statement:
With the continue statement we can stop the current iteration, and continue with the next:
Continue to the next iteration if i is 3:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Python While Loop:
The else Statement:
With the else statement we can run a block of code once when the condition no longer is
true:
Print a message once the condition is false:
i=1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Python Functions
my_function()
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses.
You can add as many arguments as you want, just separate them with a
comma.
The following example has a function with one argument (fname). When
the function is called, we pass along a first name, which is used inside the
function to print the full name:
Example
def my_function(fname):
print(fname)
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Arguments are often shortened to args in Python documentations.
Parameters or Arguments?
The terms parameter and argument can be used for the
same thing: information that are passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses
in the function definition.
An argument is the value that is sent to the function
when it is called.
Number of Arguments
By default, a function must be called with the correct
number of arguments. Meaning that if your function
expects 2 arguments, you have to call the function with
2 arguments, not more, and not less.
Example
This function expects 2 arguments, and gets 2 arguments:
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", “Franses")
Example
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Return Values
To let a function return a value, use the return statement.
Example
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Arbitrary Arguments, *args
If you do not know how many arguments that will be passed into your function, add a * before the
parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly:
Example:
def my_function(*kids):
print("The youngest child is " + kids[2])
Example:
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
The phrase Keyword Arguments are often shortened to kwargs in Python documentations.
Arbitrary Keyword Arguments, **kwargs
If you do not know how many keyword arguments that will be passed into your function, add two
asterisk: ** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items accordingly:
Example:
def my_function(**kid):
print("His last name is " + kid["lname"])
Example:
def myfunction():
pass
Recursion
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result