Skip to content

Commit 285cc0f

Browse files
author
majid shahbaz
committed
README.md updated and added functions
1 parent 07ea07e commit 285cc0f

File tree

11 files changed

+546
-0
lines changed

11 files changed

+546
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""
2+
Python Conditions and If statements
3+
Python supports the usual logical conditions from mathematics:
4+
5+
Equals: a == b
6+
Not Equals: a != b
7+
Less than: a < b
8+
Less than or equal to: a <= b
9+
Greater than: a > b
10+
Greater than or equal to: a >= b
11+
These conditions can be used in several ways, most commonly in "if statements" and loops.
12+
13+
An "if statement" is written by using the if keyword.
14+
"""
15+
a = 33
16+
b = 200
17+
if b > a:
18+
print("b is greater than a")
19+
# b is greater than a
20+
"""
21+
Indentation
22+
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.
23+
Other programming languages often use curly-brackets for this purpose.
24+
"""
25+
#If statement, without indentation (will raise an error):
26+
27+
a = 33
28+
b = 200
29+
# if b > a:
30+
# print("b is greater than a") # you will get an error
31+
"""
32+
print("b is greater than a") # you will get an error
33+
^
34+
IndentationError: expected an indented block after 'if' statement on line 29
35+
"""
36+
#Elif
37+
#The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".
38+
a = 33
39+
b = 33
40+
if b > a:
41+
print("b is greater than a")
42+
elif a == b:
43+
print("a and b are equal")
44+
45+
#a and b are equal
46+
"""
47+
Else
48+
The else keyword catches anything which isn't caught by the preceding conditions.
49+
Example
50+
"""
51+
a = 200
52+
b = 33
53+
if b > a:
54+
print("b is greater than a")
55+
elif a == b:
56+
print("a and b are equal")
57+
else:
58+
print("a is greater than b")
59+
60+
#a is greater than b
61+
62+
# Short Hand if
63+
if a > b: print("a is greater than b")
64+
65+
# Short Hand elif
66+
67+
print("a is greater than b") if a > b else print("a is less than b")
68+
# a is greater than b
69+
"""
70+
And
71+
The and keyword is a logical operator, and is used to combine conditional statements:
72+
73+
Example
74+
Test if a is greater than b, AND if c is greater than a:
75+
76+
"""
77+
a = 200
78+
b = 33
79+
c = 500
80+
if a > b and c > a:
81+
print("Both conditions are True")
82+
# Both conditions are True
83+
# Simplify version
84+
if b < a < c:
85+
print("Both conditions are True")
86+
# Both conditions are True
87+
88+
"""
89+
Or
90+
The or keyword is a logical operator, and is used to combine conditional statements:
91+
92+
Example:
93+
Test if a is greater than b, OR if a is greater than c:
94+
"""
95+
if a > b or a > c:
96+
print("A is greater than B or A greater than C")
97+
#A is greater than B or A greater than C
98+
99+
"""
100+
Not
101+
The not keyword is a logical operator, and is used to reverse the result of the conditional statement:
102+
"""
103+
if not b>a:
104+
print("B is not greater than A")
105+
#B is not greater than A
106+
107+
"""
108+
The pass Statement:
109+
110+
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
111+
"""
112+
a = 33
113+
b = 200
114+
115+
if b > a:
116+
pass
117+
else:
118+
pass
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
The match statement is used to perform different actions based on different conditions.
3+
4+
The Python Match Statement
5+
Instead of writing many if..else statements, you can use the match statement.
6+
7+
The match statement selects one of many code blocks to be executed.
8+
9+
match expression:
10+
case x:
11+
code block
12+
case y:
13+
code block
14+
case z:
15+
code block
16+
17+
This is how it works:
18+
19+
The match expression is evaluated once.
20+
The value of the expression is compared with the values of each case.
21+
If there is a match, the associated block of code is executed.
22+
23+
"""
24+
day = 3
25+
match day:
26+
case 1:
27+
print("Monday")
28+
case 2:
29+
print("Tuesday")
30+
case 3:
31+
print("Wednesday")
32+
case 4:
33+
print("Thursday")
34+
case 5:
35+
print("Friday")
36+
case 6:
37+
print("Saturday")
38+
case 7:
39+
print("Sunday")
40+
case _:
41+
print("Default")
42+
# For no match case _ will execute and it act as default case.
43+
# Default
44+
45+
"""
46+
Combine Values
47+
Use the pipe character | as an or operator in the case evaluation to check for more than one value match in one case:
48+
"""
49+
day = 4
50+
match day:
51+
case 1 | 2 | 3 | 4 | 5:
52+
print("Today is a weekday")
53+
case 6 | 7:
54+
print("I love weekends!")
55+
56+
# Today is a weekday
57+
58+
"""
59+
If Statements as Guards
60+
You can add if statements in the case evaluation as an extra condition-check:
61+
"""
62+
month = 5
63+
day = 4
64+
65+
match day:
66+
case 1 | 2 | 3 | 4 | 5 if month == 4:
67+
print("A weekday in April")
68+
case 1 | 2 | 3 | 4 | 5 if month == 5:
69+
print("A weekday in may")
70+
case _:
71+
print("No match")
72+
# A weekday in may
73+
File renamed without changes.

src/basic/functions/functions.py

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
"""
2+
A function is a block of code which only runs when it is called.
3+
You can pass data, known as parameters, into a function.
4+
A function can return data as a result.
5+
"""
6+
print("\n----------- Functions -------------\n")
7+
8+
9+
# To Create a function python use a def keyword
10+
11+
def test():
12+
print("A generic function")
13+
14+
15+
# This function runs when it is called so
16+
test()
17+
# prints -> A generic function
18+
19+
print("\n----------- Arguments passing -------------\n")
20+
"""
21+
There are many ways to pass parameters in the function such as
22+
- Positional Parameters
23+
- Arbitrary Arguments, *args
24+
- Keyword Arguments
25+
- Arbitrary Keyword Arguments, **kwargs
26+
- Positional Only Arguments
27+
- Keyword Only Arguments
28+
"""
29+
30+
31+
# By default, a function must call with correct number of parameters
32+
def user(firstname, lastname):
33+
print(f"{firstname} {lastname}")
34+
35+
36+
# Call this function
37+
user("Test", "one")
38+
39+
40+
# Prints -> Test one
41+
42+
# If we don't know the number of arguments to pass then we name them arbitrary arguments *args
43+
# Represented by *arg
44+
45+
46+
def user(*args): # User will get tuple
47+
firstname = args[0] # get the values from tuple based on the index
48+
lastname = args[1]
49+
age = args[2]
50+
print(f"User detail => {firstname} {lastname} {age}")
51+
52+
53+
# I initialize a tuple
54+
user_data = ("Test", "Two", 20)
55+
user(*user_data) # Before passing it to function I unpack it to separate the values
56+
# User detail => Test Two 20
57+
58+
"""
59+
Keyword Arguments
60+
- You can also send arguments with the key = value syntax.
61+
- This way the order of the arguments does not matter.
62+
63+
These are represented by **kwargs
64+
"""
65+
66+
67+
# user will receive a dictionary and then get the values from the dictionary based on the key values
68+
def user(**kwargs):
69+
first_name = kwargs["firstname"]
70+
last_name = kwargs["lastname"]
71+
age = kwargs["age"]
72+
print(f"Users details by **kwargs => {first_name} {last_name} {age}")
73+
74+
75+
dictionary = {"firstname": "Test", "lastname": "two", "age": 20}
76+
user(**dictionary)
77+
78+
79+
# Users details by **kwargs => Test two 20
80+
81+
# To pass a default parameter value
82+
def user(firstname="None"):
83+
print(f"Default value of firstname => {firstname}")
84+
85+
86+
user()
87+
88+
89+
# Default value of firstname => None
90+
91+
# Passing List as arguments
92+
def user(addresses):
93+
[print(f"Addresses of user {address}") for address in addresses]
94+
95+
96+
addr = ["first", "second", "third"]
97+
user(addr)
98+
"""
99+
Addresses of user first
100+
Addresses of user second
101+
Addresses of user third
102+
"""
103+
104+
105+
# Return values from function
106+
107+
def calculate_area(length, width):
108+
return length * width
109+
110+
111+
area = calculate_area(10, 20)
112+
print(f"Area is with return base test :: {area}")
113+
# Area is :: 200
114+
115+
"""
116+
The pass Statement
117+
Function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error.
118+
"""
119+
def myfunction():
120+
pass
121+
myfunction()
122+
123+
"""
124+
Positional-Only Arguments
125+
126+
- You can specify that a function can have ONLY positional arguments, or ONLY keyword arguments.
127+
- To specify that a function can have only positional arguments, add , / after the arguments:
128+
"""
129+
def cal_area(length,width,/):
130+
return length*width
131+
132+
#area = cal_area(lenght = 10,width = 20)
133+
#print(f"Area is :: {area}")
134+
135+
# We will get error because we have to pass positional arguments just not any keyword argument
136+
"""
137+
area = cal_area(lenght = 10,width = 20)
138+
Error:
139+
Traceback (most recent call last):
140+
File "/Users/macbookpro/Documents/Python Practice/python_practice_part1/src/basic/functions/functions.py", line 132, in <module>
141+
area = cal_area(lenght = 10,width = 20)
142+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
143+
TypeError: cal_area() got some positional-only arguments passed as keyword arguments: 'width'
144+
145+
"""
146+
147+
# To fix it we need to pass only positional because we have used forward slash (/) right after the end of arguments
148+
area = cal_area(10, 20)
149+
print(f"Area is with Positional-Only Arguments (/) end of args:: :: {area}")
150+
151+
# Area is :: 200
152+
153+
"""
154+
Keyword-Only Arguments
155+
156+
- To specify that a function can have only keyword arguments, add *, before the arguments:
157+
"""
158+
def calc_area(*,length,width):
159+
return length*width
160+
161+
area = calc_area(length = 10, width = 20)
162+
print(f"Area with is Keyword-Only Arguments (*) before args:: {area}")
163+
# Area with is Keyword-Only Arguments (*) before args:: 200
164+
165+
"""
166+
Combine Positional-Only and Keyword-Only
167+
You can combine the two argument types in the same function.
168+
169+
Any argument before the / , are positional-only, and any argument after the *, are keyword-only.
170+
171+
Example
172+
173+
"""
174+
def my_function(a, b, /, *, c, d):
175+
print(a + b + c + d)
176+
177+
my_function(5, 6, c = 7, d = 8)

0 commit comments

Comments
 (0)