Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computer Club - Session 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Introduction to

Python 02
Where nerdiness meets fun to unlock endless possibilities
TABLE OF CONTENTS

01 02 03
Recap Assessing Logic Branching

04 05 06
Looping with Looping with Summary
while for
How This Will Work

Give Examples
Explain

Quiz
Solve Exercise
01
Recap
What is an IDE?

An IDE, or an Integrated
Development Environment,
is a tool that allows you to
write and run code.

For this mini course, we will


be using the Replit IDE.
Types of Variables

int float bool


Represents integers or Represents floating Represents boolean
whole numbers point/decimal numbers values
eg: 5 eg: 3.21 eg: True

str NoneType List


Represents strings; Has only 1 value; Used to store multiple
words or represents the lack of a items in a single variable
alphanumerics value eg: ["abc", 34.2, True, 40]
eg: ‘Computer Club’
OTHER CONCEPTS

Input Casting
You can get a user’s input Changing variable from one
using the input() function type to another
Arithmetic Operations
Addition
a+b

Subtraction
a-b

Operations
Multiplication
a*b

Division
● Real Division: i/j
● Integer Division: i//j
Arithmetic Operations

Modulus
a%b
Other Returns the remainder of a/b

Operations
Power
a**n

Returns a to the nth power


02
Assessing Logic
Comparing Values
● == equality
● != inequality
● > greater than
● < less than
● >= greater than or equal to
● <= less than or equal to
Logic Operators

and
Returns True if both operands are
True

Operators or
Returns True if either of the
operands is True

not
Returns the inverse of the value of
the operand
Quiz 1
What is the result of the following operations?

1) 34 != 43 Answer: True

2) 18<=2 or 16%2 == 0 Answer: True

3) not True != False Answer: False

4) 22 == 22.0 Answer: True

5) 23>2 and 5//2 == 2.5 Answer: False

6) 9<=9 and not 9>0 Answer: False


Exercise
Write a program that asks the user
to input their age and checks if they
are above 18, under 64, or both,
such as follows:

Example: If the user inputs 82, the


program should print:

82>18: True
82<64: False
82>18 and 82<64: False
Helpful Tip: You can add \t or \n to your
strings to insert a tab or a new line
03
Branching
If Statements
The python if keyword is used to evaluate
a given condition (Boolean value) as True
or False, and runs the code only if the
condition(s) are met.

Its syntax look like this:

if condition:
statements to execute
OTHER CONCEPTS

else elif
If the conditions of the If the condition of the
previous if/elif statement(s) previous if statement is not
are not met, then the code in met, then the code checks for
the else block is executed. another condition to execute
the elif block.
Syntax:
Syntax:
if condition:
statements to execute if condition_1:
else: statements to execute
statements to execute elif condition_2:
statements to execute
Exercise

We have a loud talking parrot. The hour


variable is the current hour time in the
range 0..23, and the variable is_talking
represents whether or not the parrot is
talking. We are in trouble if the parrot is
talking and the hour is before 7 or after
20. Print “We are in trouble!” if we’re in
trouble.

Bonus: Print “Invalid time!” if the time


is not between 0 and 23.
04
Looping with While
While Loops
● Syntax:
declare iterator
while condition:
statements to execute
update iterator

● When the condition is True, the steps inside the


while block are executed
● When the execution is done, the condition is
checked again
● If the condition is true, repeat from the beginning
of the while branch; otherwise, move on to the
next statement

● As long as the condition is True, the expressions


inside the while block will keep on repeating
Caution!
Make sure not to forget the iterator to avoid
infinite loops
Exercise

Write a program that asks the user to


input a positive integer, and prints the
sum of all the odd integers less than or
equal to that number.

Bonus: If the user inputs anything other


than a positive integer, print “Invalid
input!”
05
Looping with For
For Loops
● Repeats the lines inside the for block until the
variable is no longer in the range.

Syntax:
○ for variable in range(range):
statements to execute

○ for variable in range(start, end, step_size):


statements to execute
Breaking out of Loops

continue
Skips current iteration and
proceeds to the next iteration
Keywords
break
Breaks out of the loop and
terminates it, moving on to the
next statement
Exercise

Write a program that prints out the


double of all the odd number from 0 till
20, skipping 7 (print “skipped”) and
stopping if the double of the number
exceeds 33 (print “break”).
06
Summary
Key Points
● Comparing Values

● Logic Operators

● If Statements

● Else and Elif Statements

● While Loops

● For Loops

● Breaking out of Loops


Quiz 2
Answer the following questions:
1) What are the 3 parameters in a Answer: start, end,
for loop? step size
2) (4*2<10 and 4 == 3) or 6 != Answer: True
3) What ‘6’is the value of i on the 3rd Answer: 2
iteration? for i in range(8): …
Answer: condition
4) How can an infinite loop be avoided?
not always true
5) n=0
while n<10: Answer: infinite
print(n) loop
n -= 1
6) 13%7<=5 Answer: False
EXERCISE

You and your date are trying to get a table at a


restaurant. The variable you is the stylishness of your
clothes, in the range 0..10, and date is the stylishness of
your date's clothes. Determine (and print) if you were
successful at getting a table (yes, no, maybe) such that:
If either of you is very stylish (8 or more) then the
result is yes. With the exception that if either of you
has style of 2 or less, then the result is no. Otherwise
the result is maybe.

Hint: Store the result in a str variable


EXERCISE

Write a program that asks the user to input a positive


integer, and squares that number by successive
additions.

Example: User inputs 5


Output: 5+5+5+5+5=25
THANKS!
Any questions?
CREDITS: This presentation template was created by
Slidesgo, incluiding icons by Flaticon, and
infographics & images by Freepik.

You might also like