0% found this document useful (0 votes)
59 views13 pages

CS50 Python Programming Lecture 1

Uploaded by

fuad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views13 pages

CS50 Python Programming Lecture 1

Uploaded by

fuad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

CS50’s Introduction to Programming with Python


OpenCourseWare

Donate  ([Link]

David J. Malan ([Link]


malan@[Link]
 ([Link]  ([Link] 
([Link]  ([Link] 
([Link]  ([Link]
 ([Link]

Lecture 1
Conditionals
if Statements
Control Flow, elif, and else
or
and
Modulo
Creating Our Own Parity Function
Pythonic
match
Summing Up

Conditionals
 Conditionals allow you, the programmer, to allow your program to make decisions: As if your
program has the choice between taking the left-hand road or the right-hand road based upon
certain conditions.
 Built within Python are a set of “operators” that are used to ask mathematical questions.
 > and < symbols are probably quite familiar to you.

[Link] 1/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

 >= denotes “greater than or equal to.”


 <= denotes “less than or equal to.”
 == denotes “equals, though do notice the double equal sign! A single equal sign would
assign a value. Double equal signs are used to compare variables.
 != denotes “not equal to.
 Conditional statements compare a left-hand term to a right-hand term.

if Statements
 In your terminal window, type code [Link] . This will create a brand new file called
“compare.”
 In the text editor window, begin with the following:

x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y:
print("x is less than y")

Notice how your program takes the input of the user for both x and y, casting them as
integers and saving them into their respective x and y variables. Then, the if statement
compares x and y. If the condition of x < y is met, the print statement is executed.
 if statements use bool or boolean values (true or false) to decide whether or not to
execute. If the statement of x > y is true, the compiler will register it as true and execute
the code.

Control Flow, elif, and else


 Further revise your code as follows:

x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y:
print("x is less than y")
if x > y:
print("x is greater than y")
if x == y:
print("x is equal to y")

Notice how you are providing a series of if statements. First, the first if statement is
evaluated. Then, the second if statement runs its evaluation. Finally, the last if statement
runs its evaluation. This flow of decisions is called “control flow.”
[Link] 2/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

 Our code can be represented as follows:

start

x<y

True

"x is less than y" False

x>y

True

"x is greater than y" False

x == y

True

"x is equal to y" False

stop

[Link] 3/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

 This program can be improved by not asking three consecutive questions. After all, not all
three questions can have an outcome of true ! Revise your program as follows:

x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")

Notice how the use of elif allows the program to make fewer decisions. First, the if
statement is evaluated. If this statement is found to be true, all the elif statements will not
be run at all. However, if the if statement is evaluated and found to be false, the first elif
will be evaluated. If this is true, it will not run the final evaluation.
 Our code can be represented as follows:

[Link] 4/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

start

x<y

False

x>y

True False

True x == y

True

"x is less than y" "x is greater than y" "x is equal to y" False

stop

 While your computer may not notice a difference speed-wise between our first program and
this revised program, consider how an online server running billions or trillions of these
types of calculations each day could definitely be impacted by such a small coding decision.
 There is one final improvement we can make to our program. Notice how logically elif x ==
y is not a necessary evaluation to run. After all, if logically x is not less than y AND x is not
greater than y, x MUST equal y. Therefore, we don’t have to run elif x == y . We can create a
“catch-all,” default outcome using an else statement. We can revise as follows:

x = int(input("What's x? "))
y = int(input("What's y? "))

[Link] 5/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x is equal to y")

Notice how the relative complexity of this program has decreased through our revision.
 Our code can be represented as follows:

start

x<y

False

True x>y

True False

"x is less than y" "x is greater than y" "x is equal to y"

stop

or
 or allows your program to decide between one or more alternatives. For example, we could
further edit our program as follows:

x = int(input("What's x? "))
y = int(input("What's y? "))

if x < y or x > y:

[Link] 6/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python
print("x is not equal to y")
else:
print("x is equal to y")

Notice that the result of our program is the same, but the complexity is decreased. The
efficiency of our code is increased.
 At this point, our code is pretty great. However, could the design be further improved? We
could further edit our code as follows:

x = int(input("What's x? "))
y = int(input("What's y? "))

if x != y:
print("x is not equal to y")
else:
print("x is equal to y")

Notice how we removed the or entirely and simply asked, “Is x not equal to y?” We ask one
and only one question. Very efficient!
 For the purpose of illustration, we could also change our code as follows:

x = int(input("What's x? "))
y = int(input("What's y? "))

if x == y:
print("x is equal to y")
else:
print("x is not equal to y")

Notice that the == operator evaluates if what is on the left and right are equal to one
another. The use of double equal signs is very important. If you use only one equal sign, an
error will likely be thrown by the compiler.
 Our code can be illustrated as follows:

[Link] 7/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

start

x == y

True False

"x is equal to y" "x is not equal to y"

stop

and
 Similar to or , and can be used within conditional statements.
 Execute in the terminal window code [Link] . Start your new program as follows:

score = int(input("Score: "))

if score >= 90 and score <= 100:


print("Grade: A")
elif score >=80 and score < 90:
print("Grade: B")
elif score >=70 and score < 80:
print("Grade: C")
elif score >=60 and score < 70:
print("Grade: D")
else:
print("Grade: F")

Notice that by executing python [Link] , you will be able to input a score and get a grade.
However, notice how there is potential for bugs.
 Typically, we do not want to ever trust our users to input the correct information. We could
improve our code as follows:

score = int(input("Score: "))

if 90 <= score <= 100:

[Link] 8/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python
print("Grade: A")
elif 80 <= score < 90:
print("Grade: B")
elif 70 <= score < 80:
print("Grade: C")
elif 60 <= score < 70:
print("Grade: D")
else:
print("Grade: F")

Notice how Python allows you to chain together the operators and conditions in a way quite
uncommon to other programming languages.
 Still, we can further improve our program:

score = int(input("Score: "))

if score >= 90:


print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")

Notice how the program is improved by asking fewer questions. This makes our program
easier to read and far more maintainable in the future.
 You can learn more in Python’s documentation on control flow
([Link]

Modulo
 In mathematics, parity refers to whether a number is either even or odd.
 The modulo % operator in programming allows one to see if two numbers divide evenly or
divide and have a remainder.
 For example, 4 % 2 would result in zero, because it evenly divides. However, 3 % 2 does not
divide evenly and would result in a number other than zero!
 In the terminal window, create a new program by typing code [Link] . In the text editor
window, type your code as follows:

x = int(input("What's x? "))

if x % 2 == 0:
print("Even")

[Link] 9/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python
else:
print("Odd")

Notice how our users can type in any number 1 or greater to see if it is even or odd.

Creating Our Own Parity Function


 As discussed in Lecture 0, you will find it useful to create a function of your own!
 We can create our own function to check whether a number is even or odd. Adjust your code
as follows:

def main():
x = int(input("What's x? "))
if is_even(x):
print("Even")
else:
print("Odd")

def is_even(n):
if n % 2 == 0:
return True
else:
return False

main()

Notice that our if statement is_even(x) works even though there is no operator there.
This is because our function returns a bool (or boolean), true or false, back to the main
function. The if statement simply evaluates whether or not is_even of x is true or false.

Pythonic
 In the programming world, there are types of programming that are called “Pythonic” in
nature. That is, there are ways to program that are sometimes only seen in Python
programming. Consider the following revision to our program:

def main():
x = int(input("What's x? "))
if is_even(x):
print("Even")
else:
print("Odd")

def is_even(n):
return True if n % 2 == 0 else False
[Link] 10/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

main()

Notice that this return statement in our code is almost like a sentence in English. This is a
unique way of coding only seen in Python.
 We can further revise our code and make it more and more readable:

def main():
x = int(input("What's x? "))
if is_even(x):
print("Even")
else:
print("Odd")

def is_even(n):
return n % 2 == 0

main()

Notice that the program will evaluate what is happening within the n % 2 == 0 as either
true or false and simply return that to the main function.

match
 Similar to if , elif , and else statements, match statements can be used to conditionally
run code that matches certain values.
 Consider the following program:

name = input("What's your name? ")

if name == "Harry":
print("Gryffindor")
elif name == "Hermione":
print("Gryffindor")
elif name == "Ron":
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")

Notice the first three conditional statements print the same response.
 We can improve this code slightly with the use of the or keyword:

name = input("What's your name? ")

[Link] 11/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python
if name == "Harry" or name == "Hermione" or name == "Ron":
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")

Notice the number of elif statements has decreased, improving the readability of our code.
 Alternatively, we can use match statements to map names to houses. Consider the following
code:

name = input("What's your name? ")

match name:
case "Harry":
print("Gryffindor")
case "Hermione":
print("Gryffindor")
case "Ron":
print("Gryffindor")
case "Draco":
print("Slytherin")
case _:
print("Who?")

Notice the use of the _ symbol in the last case. This will match with any input, resulting in
similar behavior as an else statement.
 A match statement compares the value following the match keyword with each of the values
following the case keywords. In the event a match is found, the respective indented code
section is executed, and the program stops the matching.
 We can improve the code:

name = input("What's your name? ")

match name:
case "Harry" | "Hermione" | "Ron":
print("Gryffindor")
case "Draco":
print("Slytherin")
case _:
print("Who?")

Notice, the use of the single vertical bar | . Much like the or keyword, this allows us to
check for multiple values in the same case statement.

Summing Up

[Link] 12/13
1/5/24, 2:24 PM Lecture 1 - CS50's Introduction to Programming with Python

You now have the power within Python to use conditional statements to ask questions and have
your program take action accordingly. In this lecture, we discussed…

 Conditionals;
 if Statements;
 Control flow, elif , and else ;
 or ;
 and ;
 Modulo;
 Creating your own function;
 Pythonic coding;
 and match .

[Link] 13/13

Common questions

Powered by AI

Using 'elif' in Python improves the efficiency of conditional statements by reducing the number of evaluations and simplifying decision-making. When using multiple 'if' statements, each condition is checked independently, whereas 'elif' stops further checks upon finding a true condition, reducing unnecessary operations .

The modulo operator (%) helps determine parity by returning the remainder of a division. For example, 'n % 2 == 0' checks whether a number 'n' is even by verifying if the remainder of 'n' divided by 2 is zero. If true, the number is even; otherwise, it is odd. This method avoids conditional branching and leverages arithmetic directly for determining parity .

Optimizing code by reducing conditional checks enhances maintainability by making logic more straightforward and transparent, allowing future developers to understand and modify code efficiently. Performance improves as the execution path simplifies, lessening computational overhead, particularly in high-volume environments where every unnecessary check impacts overall speed .

The Python 'match' statement enhances readability by directly associating input values with corresponding outcomes in a declarative manner. It simplifies code by consolidating multiple cases into a single construct, which is cleaner than a series of 'elif' statements. 'Match' also allows for grouping similar cases with the '|' operator, which eliminates redundancy and improves maintainability .

Using double equal signs (==) for comparisons is crucial because it distinguishes between assignment and comparison operations. A single equal sign '=' assigns a value to a variable, whereas '==' compares two values for equality, which prevents logical errors and ensures correct operation flow in conditional statements .

The 'else' statement provides a catch-all logic that simplifies the program by eliminating redundant checks. If none of the preceding conditions are true, 'else' automatically handles the remaining case without needing an explicit condition, which can reduce logical errors and improve code readability .

Chaining comparison operators like 'if 90 <= score <= 100' benefits readability and succinctness by combining multiple comparisons into a single coherent statement. This reduces the need for logical operators such as 'and', decreases potential for logic errors, and aligns closely with mathematical notation for easier understanding .

Removing 'if x < y and x > y' and using 'if x != y' simplifies logic by reducing redundancy and unnecessary condition checks. The former condition is logically incorrect as x cannot be both less than and greater than y simultaneously, whereas 'x != y' directly checks for inequality accurately and performs better by minimizing comparisons to a single straightforward condition .

The Pythonic way of defining functions enhances both efficiency and readability by using concise return statements that operate like simple English sentences. This reduces the need for verbose conditional logic within the function, making the code clearer and more direct. For example, the use of 'return n % 2 == 0' in the 'is_even' function avoids unnecessary if-else structure and directly returns a boolean value .

Employing the 'or' logical operator allows combining multiple conditional cases into a single line. This reduces the number of 'elif' statements needed by checking multiple possibilities at once. For example, 'if name == "Harry" or name == "Hermione"' consolidates what could otherwise be separate 'elif' statements into one efficient condition .

You might also like