-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyBasicFunctions.py
More file actions
99 lines (67 loc) · 3.37 KB
/
pyBasicFunctions.py
File metadata and controls
99 lines (67 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Return multiple Values
def generate_list(name, num_elements=2): # 2 as default value for num_elements argument
return [name for _ in range(num_elements)] # comprehension in return statements
some_list = generate_list("techecoder", 5) # Function calling using Positional arguments
another_list = generate_list("github", num_elements=6) # Keyword arg should always followed by Positional arg
print(type(some_list)) # type of return should be list, Default return in python is always NONE
print(some_list) # print tuple of names
print(another_list)
# functions with variable arguments,
def students_in_college(college, city1, *students):
print("My name is", college)
print("my college name is: ", city1)
print("The students enrolled are:", students)
list12 = ["Techie", "Coder"]
students_in_college("IIT", "India", "Techie", "Coder")
students_in_college("IIT", "India", *list12) # We have to add * if passing list as students.
# Function calling multiple Keyword arguments
def student_details(**details):
for key, value in details.items():
print(key, value)
detail_dict = {'name': "teche", 'age': 18, 'college': "boston university"}
student_details(**detail_dict) # We have to add ** if passing dict as arguments.
# function to call both variable positional and keyword arguments
def student_in_college(*student_detail, **college):
print("Students--")
for i in student_detail:
print(i)
print()
print("College Details--")
for key, value in college.items():
print(key, value)
student_in_college("Alison", "Bob", "Charlie", name="stanford", city="LA") # Calling both positional and Keyword args
# Function to return multiple values
def add_sub(x, y):
add_results = x + y
sub_results = x - y
return add_results, sub_results
tuple13 = add_sub(6, 5) # return as tuple
result_1, result2 = add_sub(8, 4)
a, _ = add_sub(9, 2) # Put _ if second return value not being used
print(tuple13, result_1, result2, a)
# Global and local variables in function
country = "USA" # Global variable
city = "Seattle" # Global variable
def some_fn():
country1 = "India" # Local variable override global variable
global city # define city as global variable in case same name local variable exist in function
city = city + "-City" # defining local city variable using Global city variable
print("Country: ", country1) # Local value override here and print "India"
print("City: ", city) # Global variable value Seattle print here
some_fn()
# Function Argument passing by value: Impact on only inner print
fruit_list = ["Apple", "Banana"] # Define Global variable
def change_list1(fruit_list):
fruit_list = ["Mango", "Banana"] # Define new local list
print("Inside the function passing by value: ", fruit_list) # This will print updated list
change_list1(fruit_list)
print("Outside the function passing by value: ", fruit_list) # Print global list values only
# Function Argument passing by Reference: Update global List as well.
def change_list2(fruit_list):
fruit_list[1] = "Kiwi"
fruit_list.append("grapes")
print("Inside the function passing by Reference: ", fruit_list)
change_list2(fruit_list)
print("Outside the function passing by Reference: ",
fruit_list) # Print updated List as Global list has been modified in function
# help("modules") this will give all python modules installed in pycharm