Skip to main content

Posts

Showing posts with the label Python

Python Loops

Python Loops In Python, you can use loops to repeatedly execute a block of code. There are two main types of loops: for loops and while loops.   for loop: The for loop is used to iterate over a sequence (such as a list, tuple, string, or range). Example:  # Iterating over a list     fruits = [ "apple" , "banana" , "cherry" ]      for fruit in fruits:        print (fruit) # Iterating over a range    For i in range ( 5 ):       print (i)   while loop : The while loop is used to repeatedly execute a block of code as long as a given condition is true. Example: # Using a while loop to print numbers from 0 to 4 count = 0 while count < 5 :     print (count) count += 1   # Increment the counter Loop Control Statements: Python also provides loop control statements to modify the execution of loops: break : Terminates the loop and transfers control to the statement imme...

Python tuple Function & Methods

  Python tuple Function & Methods In Python, tuples are immutable, meaning their elements cannot be modified once the tuple is created. Consequently, there are only a few built-in methods specific to tuples, as they are more limited compared to mutable types like lists. However, there are some functions and operations that can be performed on tuples. Here are some key functions and methods related to tuples in Python: Built-in Functions: len(): Returns the length (number of elements) of a tuple. my_tuple = ( 1 , 2 , 3 , 'a' , 'b' , 'c' ) length = len (my_tuple) print (length) # Output: 6   max() and min(): Returns the maximum or minimum element in the tuple. numbers = ( 10 , 5 , 8 , 20 ) max_value = max (numbers) min_value = min (numbers) print (max_value, min_value) # Output: 20 5   sum(): Returns the sum of all elements in a tuple (if all elements are numeric). numbers = ( 1 , 2 , 3 , 4 , 5 ) total = sum (numbers) ...