Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7806625

Browse files
authored
Update and rename les10_arrays_part1.py to les13_functions.py
Functions
1 parent daa6e9d commit 7806625

File tree

2 files changed

+125
-100
lines changed

2 files changed

+125
-100
lines changed

‎les10_arrays_part1.py‎

Lines changed: 0 additions & 100 deletions
This file was deleted.

‎les13_functions.py‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#https://newdigitals.org/2024/01/23/basic-python-programming/#functions
2+
#Functions
3+
'''
4+
Python Function is a block of statements that return the specific task.
5+
The idea is to put some commonly or repeatedly done tasks together and make a function so that instead
6+
of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again.
7+
There are mainly two types of functions in Python.
8+
Built-in library function: These are Standard functions in Python that are available to use.
9+
User-defined function: We can create our own functions based on our requirements.
10+
We can define a function using the def keyword
11+
'''
12+
# A simple Python function
13+
def fun():
14+
print("Welcome to Python")
15+
16+
17+
# Driver code to call a function
18+
fun()
19+
Output:
20+
Welcome to Python
21+
'''
22+
You can pass data, known as parameters, into a function.
23+
A function can return data as a result.
24+
Information can be passed into functions as arguments.
25+
Arguments are specified after the function name, inside the parentheses.
26+
You can add as many arguments as you want, just separate them with a comma:
27+
'''
28+
def add(num1: int, num2: int) -> int:
29+
"""Add two numbers"""
30+
num3 = num1 + num2
31+
32+
return num3
33+
34+
# Driver code
35+
num1, num2 = 5, 15
36+
ans = add(num1, num2)
37+
print(f"The addition of {num1} and {num2} results {ans}.")
38+
Output:
39+
The addition of 5 and 15 results 20.
40+
# some more functions
41+
def is_prime(n):
42+
if n in [2, 3]:
43+
return True
44+
if (n == 1) or (n % 2 == 0):
45+
return False
46+
r = 3
47+
while r * r <= n:
48+
if n % r == 0:
49+
return False
50+
r += 2
51+
return True
52+
print(is_prime(78), is_prime(79))
53+
54+
Output:
55+
False True
56+
57+
#If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.
58+
def my_function(*kids):
59+
print("The youngest child is " + kids[2])
60+
61+
my_function("Emil", "Tobias", "Linus")
62+
Output:
63+
The youngest child is Linus
64+
65+
#Keyword Arguments: You can also send arguments with the key = value syntax
66+
67+
def my_function(child3, child2, child1):
68+
print("The youngest child is " + child3)
69+
70+
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
71+
The youngest child is Linus
72+
73+
#If you do not know how many keyword arguments that will be passed into your function,
74+
#add two asterisk: ** before the parameter name in the function definition.
75+
def my_function(**kid):
76+
print("His last name is " + kid["lname"])
77+
78+
my_function(fname = "Tobias", lname = "Refsnes")
79+
Output:
80+
His last name is Refsnes
81+
'''
82+
You can send any data types of argument to a function (string, number, list, dictionary etc.),
83+
and it will be treated as the same data type inside the function.
84+
E.g. if you send a List as an argument, it will still be a List when it reaches the function:
85+
'''
86+
def my_function(food):
87+
for x in food:
88+
print(x)
89+
90+
fruits = ["apple", "banana", "cherry"]
91+
92+
my_function(fruits)
93+
94+
Output:
95+
apple
96+
banana
97+
cherry
98+
99+
#To let a function return a value, use the return statement:
100+
def my_function(x):
101+
return 5 * x
102+
103+
print(my_function(3))
104+
print(my_function(5))
105+
print(my_function(9))
106+
Output:
107+
15
108+
25
109+
45
110+
111+
#A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.
112+
113+
#Add 10 to argument a, and return the result:
114+
x = lambda a : a + 10
115+
print(x(5))
116+
Output:
117+
15
118+
119+
#Lambda functions can take any number of arguments:
120+
121+
#Multiply argument a with argument b and return the result:
122+
x = lambda a, b : a * b
123+
print(x(5, 6))
124+
Output:
125+
30

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /