Skip to content

Commit 89a7f3d

Browse files
Added examples and comments while learning conditionals
1 parent b4486c9 commit 89a7f3d

1 file changed

Lines changed: 78 additions & 1 deletion

File tree

basics/conditionals.py

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
11
#----------Conditionals with Python---------
2-
print("Lets learn conditionals")
2+
print("Lets learn conditionals")
3+
print("-----------------------")
4+
5+
# What is conditionals?
6+
7+
# Conditionals in Python are statements that allow a program to make decisions and execute different blocks of code based on whether a specified condition is true or false.
8+
# They are fundamental for controlling the flow of a program and enabling it to respond dynamically to various inputs or situations.
9+
10+
# Conditionals:
11+
12+
# if
13+
# elif
14+
# else
15+
16+
# How they work
17+
18+
#----------------Example 1-----------------------
19+
20+
# We store the users input in the variable called <name>
21+
name = input("What is your name: ")
22+
if name == "John": # if the user name is equal to john we will print a message "Hello john"
23+
print(f"Hello {name}")
24+
else: # If the users name is not john we will print a message letting the program know john is not present
25+
print(f"I only say hello if john is present")
26+
27+
#---------------Example 2-----------------------
28+
29+
age = input("Enter your age ")
30+
if age >= "18":
31+
print("You are allowed to vote")
32+
else:
33+
print("Please come back when you are 18 or over")
34+
35+
#---------------Elif--(elseif)---------------
36+
37+
# The elif conditional basically means (else if) , for example: if name is John or Mary or Sam, then do this. If not, do something else
38+
# We would say (if) your name is John (elif) Mary (elif) Sam then do this or (else) do something else
39+
# We will also use the statement (or) to make sure it doesnt matter if the first letter is uppercase or lowercase
40+
41+
#----------------Example----------------------
42+
# In this example is will be using:
43+
44+
# Variables
45+
# Print
46+
# if
47+
# elif
48+
# else
49+
# or
50+
# f string
51+
52+
print("The only people allowed to Access are:\n")
53+
print("John, Mary, Sam") # We print out the names on the screen for those who are allowed to enter
54+
j = "John" # We store all the user names in variables so we can use them later
55+
m = "Mary"
56+
s = "Sam"
57+
names = input("Enter your name to Access: ") # Ask the user to input their name
58+
if names == "john" or names == "John": # if the name is john or John
59+
print(f"You have Access {j}") # We will print you have Access john
60+
elif names == "sam" or names == "Sam": # if the name is sam or Sam
61+
print(f"You have Access {s}") # We will print you have Access Sam
62+
elif names == "mary" or names == "Mary": # if the name is mary or Mary
63+
print(f"You have Access {m}") # We will print you have Access Mary
64+
else:
65+
print("Access Denied") # If none of the names match the names shown above we will then print out Access Denied
66+
67+
#---------------------------Example using Boolean----
68+
print("light on or off?") # print light on or off on the screen for the user to see
69+
light_on = input("> ") # variable called light_on stores the value or data the user inputs
70+
if light_on == "on": # if the user types "on" meaning the light is on we will print the bool True
71+
print(True)
72+
elif light_on == "off": # if the user types "off" meaning the light is off we will print the bool False
73+
print(False)
74+
75+
76+
77+
78+
79+

0 commit comments

Comments
 (0)