Tuesday, 20 February 2024

Python Learning Session –05- Loops and Iteration

 

Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

Loops (repeated steps) have iteration variables that change each time through a loop.  Often these iteration variables go through a sequence of numbers.

There are so many type of looping statement are available to doing various operations

 

Python has two primitive loop commands:

  • while loops
  • for loops

 

While Loops Are:

·         The while Loop

·         The break Statement

·         The continue Statement

·         The else Statement

For Loops Are: -

·         Looping Through a String

·         The break Statement

·         The continue Statement

·         The range() Function

·         Else in For Loop

·         Nested Loops

·         The pass Statement

  

lest we first understand with use case diagram the how the loop works:

 




 

 

Lest we understanding the all of methods one by one in brief are mentioned here

1)     While Loop :

 

The while loop are used in conditional looping i.e when the condition are true then executed the set of statement are alog with condition are masoned

 

Lest we more getting clearance about the syntax of the while loop

 

Code:-

 

x = 1

while x < 5:

print (x)

x += 1




·         The break Statement:

 

·         The break statement ends the current loop and jumps to the statement immediately following the loop

·         It is like a loop test that can happen anywhere in the body of the loop

Code : -

 

while True:

    line = input('> ')

    if line == 'done' :

        break

    print(line)

print(“Done!”)

 

lest we understand with use case diagram how the code is work,

 



The continue Statement : -

 

With the continue statement we can stop the current iteration, and continue with the next: The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration

Code : -

 

while True:

    line = input('> ')

    if line[0] == '#' :

        continue

    if line == 'done' :

        break

    print(line)

print('Done!')

 

 

lest we understand the continue loops is work

 



·         The else Statement :  ( Repeated Steps )

 

With the else statement we can run a block of code once when the condition no longer is true:

Loops (repeated steps) have iteration variables that change each time through a loop.  Often these iteration variables go through a sequence of numbers.

 

Code :

 

n = 5

while n > 0 :

    print(n)

    n = n – 1

print('Techshoot03!')

print(n)

 

 

 

Indefinite Loops : -

 

         While loops are called “indefinite loops” because they keep going until  a logical condition becomes False

         The loops we have seen so far are pretty easy to examine to see if they will terminate or if they will be “infinite loops”

         Sometimes it is a little harder to be sure if a loop will terminate

 

 

 

         Quite often we have a list of items of the lines in a file - effectively a finite set of things

         We can write a loop to run the loop once for each of the items in a set using the Python for construct

         These loops are called “definite loops” because they execute an exact number of times

 

A Definite Loop with Strings: -

 

In the type of the program user the String value instant of any logic or any of characters ( Special character , numbers )

 

 

 

Code : -

 

frnd = ["encky""mincky""chincky"]
for in frnd:
  
print(x)

The range() Function :

To loop through a set of code a specified number of times, we can use the range() function,

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Using the range() function:

Code: -

for x in range(9):
  print
(x)

 

The is and is not Operators

         Python has an is operator that can be used in logical expressions

         Implies “is the same as”

         Similar to, but stronger than ==

         is not  also is a logical operator

 

Code : -

smallest = None

print('Before')

for value in [13, 141, 112, 19, 714, 115] :

    if smallest is None :

        smallest = value

    elif value < smallest :

        smallest = value

    print(smallest, value)

print('After', smallest)

 


Thursday, 15 February 2024

Python Learning Session-04 - Functions

 Function Calls

 

In the context of programming, a function is a named sequence of statements that

performs a computation. When you define a function, you specify the name and

the sequence of statements. Later, you can “call” the function by name. We have

already seen one example of a function call:

 

>>> type(32)

<class 'int'>

 

The name of the function is type. The expression in parentheses is called the

argument of the function. The argument is a value or variable that we are passing

into the function as input to the function. The result, for the type function, is the

type of the argument.

It is common to say that a function “takes” an argument and “returns” a result.

The result is called the return value.

Use Case Diagram of Function for more understanding of the function




 

Python provides a number of important built-in functions that we can use without

needing to provide the function definition. The creators of Python wrote a set of

functions to solve common problems and included them in Python for us to use.



Python Functions:

         In Python a function is some reusable code that takes arguments(s) as input, does some computation, and then returns a result or results

         We define a function using the def reserved word

         We call/invoke the function by using the function name, parentheses, and arguments in an expression

         There are two kinds of functions in Python.

-  Built-in functions that are provided as part of Python - print(), input(), type(), float(), int() ...

-  Functions that we define ourselves and then use

         We treat function names as “new” reserved words
(i.e., we avoid them as variable names)

 





 

Type Conversions:

         When you put an integer and floating point in an expression, the integer is implicitly converted to a float

         You can control this with the built-in functions int() and float()

 



String Conversions:

         You can also use int() and float() to convert between strings and integers

         You will get an error if the string does not contain numeric characters



Building our Own Functions:

         We create a new function using the def keyword followed by optional parameters in parentheses

         We indent the body of the function

         This defines the function but does not execute the body of the function



Definitions and Uses:

         Once we have defined a function, we can call (or invoke) it
as many times as we like

         This is the store and reuse pattern

Arguments:-

         An argument is a value we pass into the function as its input when we call the function

         We use arguments so we can direct the function to do different kinds of work when we call it at different times

         We put the arguments in parentheses after the name of the function

         big = max('techshoot03')

 

 

 

 

Parameters:-

A parameter is a variable which we use in the function definition.  It is a “handle” that allows the code in the function to access the arguments for a particular function invocation.

>>> def greet(lang):

...     if lang == 'es':

...        print('Hola')

...     elif lang == 'fr':

...        print('Bonjour')

...     else:

...        print('Hello')

...

>>> greet('en')

Hello

>>> greet('es')

Hola

>>> greet('fr')

Bonjour

>>>

 

Return Values: -

Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression.  The return keyword is used for this.

def greet():

    return "Hello"

print(greet(), "Glenn")

print(greet(), "Sally")

 

>>> def greet(lang):

...     if lang == 'es':

...         return 'Hola'

...     elif lang == 'fr':

...         return 'Bonjour'

...     else:

...         return 'Hello'

...

>>> print(greet('en'),'Glenn')

Hello Glenn

>>> print(greet('es'),'Sally')

Hola Sally

>>> print(greet('fr'),'Michael')

Bonjour Michael

>>>

 

 

Multiple Parameters / Arguments : -

         We can define more than one parameter in the function definition

         We simply add more arguments when we call the function

         We match the number and order of arguments and parameters



Void (non-fruitful) Functions:-

         When a function does not return a value, we call it a “void” function

         Functions that return values are “fruitful” functions

         Void functions are “not fruitful”

 

To function or not to function...

 

         Organize your code into “paragraphs” - capture a complete thought and “name it”

         Don’t repeat yourself - make it work once and then reuse it

         If something gets too long or complex, break it up into logical chunks and put those chunks in functions

         Make a library of common stuff that you do over and over - perhaps share this with your friends...

         Functions

         Built-In Functions

         Type conversion (int, float)

         String conversions

         Parameters

         Arguments

         Results (fruitful functions)

         Void (non-fruitful) functions

         Why use functions?