uday_codes_python_2
uday_codes_python_2
While Loop : Allows us to execute a block of code several times as long as the condition is True.
a = 1
while a < 3:
a = a + 1
print (a)
# Output is:
2
3
Range: Generates a sequence of integers starting from 0. Stops before n (n is not included ).
Syntax: range(n)
for number in range (3):
print (number )
# Output is:
0
1
2
Range with Start and End : Generates a sequence of numbers starting from the start. Stops before the end (the end is not
included).
Syntax: range(start, end)
for number in range (5, 8):
print (number )
# Output is:
5
6
7
List: List is the most versatile python data structure. Holds an ordered sequence of items.
# Output is:
5
Six
8.2
Extended Slicing : Similar to string extended slicing, we can extract alternate items using the step.
list_a = ["R", "B", "G", "O", "W"]
list_b = list_a [0:5:3]
print(list_b ) # ['R', 'O']
Reversing a List : -1 for step will reverse the order of items in the list.
list_a = [5, 4, 3, 2, 1]
list_b = list_a [::-1]
print(list_b ) # [1, 2, 3, 4, 5]
Slicing With Negative Index : You can also specify negative indices while slicing a List.
list_a = [5, 4, 3, 2, 1]
list_b = list_a [-3:-1]
print(list_b ) # [3, 2]
Negative Step Size: Negative Step determines the decrement between each index for slicing. The start index should be
greater than the end index in this case
list_a = [5, 4, 3, 2, 1]
list_b = list_a[4:2:-1]
print(list_b) # [1, 2]
Name Usage
in By using the in operator, one can determine if a value is present in a sequence or not. not in By
using the, not in operator, one can determine if a value is not present in a sequence or not.
Nested Lists : A list as an item of another list.
List Methods
extend() list_a.extend(list_b) Adds all the elements of a sequence to the end of the list.
remove() list.remove(value) Removes the first matching element from the list.
index() list.index(value) Returns the index at the first occurrence of the specified value.
count() list.count(value) Returns the number of elements with the specified value.
copy() list.copy() Returns a new list. It doesn't modify the original list.
Functions
Calling a Function : The functional block of code is executed only when the function is called.
def function_name ():
reusable code
function_name ()
sum_of_two_number (2, 3)
def function_name(args):
reusable code
function_name(args)
Returning a Value: To return a value from the function use return keyword. Exits from the function when return statement is
executed.
def function_name(args):
block of code
return msg
function_name(args)
result = sum_of_two_number(2, 3)
print(result) # 5
Positional Arguments : Values can be passed without using argument names. These values get assigned according to their
position. Order of the arguments matters here.
def greet (arg_1 , arg_2 ):
print (arg_1 + " " + arg_2 ) # Good Morning Ram
Default Values :
def greet (arg_1 ="Hi" , arg_2 ="Ram" ):
print (arg_1 + " " + arg_2 ) # Hi Ram
Arbitrary Function Arguments: We can define a function to receive any number of arguments.
more_args (1, 2, 3, 4)
Unpacking as Arguments : If we already have the data required to pass to a function as a sequence, we can unpack it with *
while passing.
def greet (arg1 ="Hi" , arg2 ="Ram" ):
print (arg1 + " " + arg2) # Hello Teja
Multiple Keyword Arguments : We can define a function to receive any number of keyword arguments. Variable length
kwargs are packed as dictionary.
def more_args (**kwargs ):
print (kwargs ) # {'a': 1, 'b': 2}
a = int(input ()) # 5
increment (a)
print(a) # 5
Even though variable names are same, they are referring to two different objects.
Changing the value of the variable inside the function will not affect the variable outside.
add_item (list_a )
print(list_a ) # [1, 2, 3]
The same object in the memory is referred by both list_a and list_x
add_item ()
add_item ([1,2])
add_item ()
# Output is:
[3]
[1, 2, 3]
[3, 3]
Default args are evaluated only once when the function is defined, not each time the function is called.
Nested Loops
Nested Loops : An inner loop within the repeating block of an outer loop is called a Nested Loop . The Inner Loop will be
executed one time for each iteration of the Outer Loop.
Syntax:
for item in sequence A :
Block 1
for item in sequence B :
Block 2
Name Usage
Break break statement makes the program exit a loop early.
Continue continue is used to skip the remaining statements in the current iteration when a condition is
satisfied.
Pass pass statement is used as a syntactic placeholder. When it is executed, nothing happens.
Name Usage
Break (In Nested break in the inner loop stops the execution of the inner loop.
Loop)