python

My Python Notes(5)

Python Essentials 1 Tuples Two tuples, each containing four elements. A tuple is an immutable sequence type. It can behave like a list, but it can’t be modified in situ. tuple_1 = (1, 2, 4, 8) tuple_2 = 1., .5, .25, .125 Each tuple element may be of a different type. Tupple’s stuff empty_tuple = () single_valaue_tuple = (1,) # the comma is required. print(empty_tuple) print(single_valaue_tuple) The output would be:

My Python Notes(4)

Python Essentials 1 Recursion A function can call other functions, or even itself. When a function calls itself, this situation is known as recursion, and the function which calls itself and contains a specified termination condition (i.e., the base case − a condition which doesn’t tell the function to make any further calls to that function) is called a recursive function. You can use recursive functions in Python to write clean, elegant code, and divide it into smaller, organized chunks.

My Python Notes(3)

Python Essentials 1 loop - else Python allows the else keyword to be used with the for and while loops too. The else block appears after the body of the loop. The statements in the else block will be executed after all iterations are completed. The program exits the loop only after the else block is executed. for i in range(1): print("#") else: print("#") Output: # # Functions def message(number): print("Enter a number:", number) Positional Paramenter Passing # Example 1 def my_function(a, b, c): print(a, b, c) my_function(1, 2, 3) # Example 2 def introduction(first_name, last_name): print("Hello, my name is", first_name, last_name) introduction("Luke", "Skywalker") introduction("Jesse", "Quick") introduction("Clark", "Kent") Keyword Parameter Passing # Example 1 def introduction(first_name, last_name): print("Hello, my name is", first_name, last_name) introduction(first_name = "James", last_name = "Bond") introduction(last_name = "Skywalker", first_name = "Luke") Mixing Positional and Keyword Parameter Passing adding(3, c = 1, b = 2) The previous is accepted and as long as you put the positional arguments before keyword arguments.

My Python Notes(2)

Python Essentials 1 Sorting my_list = [8,10,6,2,4] swapped = True while swapped: swapped = False for i in range(len(my_list)-1): if my_list[i] > my_list[i+1]: swapped = True my_list[i], my_list[i+1] = my_list[i+1], my_list[i] print(my_list) Bubble Sort - Interactive Mode my_list = [] swapped = True num = int(input("How many elements do you want to sort: ")) for i in range(num): val = float(input("Enter a list element: ")) my_list.append(val) while swapped: swapped = False for i in range(len(my_list) - 1): if my_list[i] > my_list[i + 1]: swapped = True my_list[i], my_list[i + 1] = my_list[i + 1], my_list[i] print("\nSorted:") print(my_list) Sort Method my_list = [8, 10, 6, 2, 4] my_list.

My Python Notes

Python Essentials 1 One more time, these notes are not meant to be useful for anyone else but me. I am not caring that much about formatting or presentation. I am posting it as a record that I am doing something (which motivates me to keep learning) and as future quick reference instead of going to Google. ( Like this at least I visit my blog :( ) So, here it is…