Python 101
Operators
print(10//3)
print(10/3)
Operators
Operators
print(7%3)
Operators
Printing a variable
You can print a variable like we did previously you can put the variable name inside
the round brackets. print(variable_name) But sometimes you want to print text before
the variable. For example you might want to have have a variable age where we age
the user their age and then we want to print ‘I am’’ age “years old’
age = input(“How old are
you?”)
print(‘I am ‘ + age + ‘years
old’)
Lists
lists are like special containers that can hold multiple
values at once. Imagine it as a magical bag where you can
keep different things together.
Lists
If statements
imagine you have a choice to make, like whether to go out
with your friends or stay at home and watch a movie. You
can use an "if statement" to help you decide what to do.
"If it's raining outside (the condition), THEN I'll stay at home
and watch a movie (the action)." But, "IF it's not raining (the
condition is false), THEN I'll go out with my friends (the
other action)."
So, an "if statement" helps you make choices based on
whether certain conditions are true or false. If the condition
is true, you do one thing; if it's false, you do something else.
Indentation
When you write an if statement, you're saying, "Hey, computer, if this condition is
true, do the things inside this box (indented lines). Otherwise, do the things in the
other box (lines without indentation)."
If Statements
If statements
Let's say you have a variable called
"age," and you want to check if a
person is old enough to vote. If they
are 18 or older, they can vote,
otherwise, they can't.
Age is a variable that asks for your
age. If your age is 18 and older then it
will print “You are old enough to
vote!” If you age is less than 18 then it
will print “"You are not old enough to
vote yet."”
If statements
For loops
Imagine you have a list of your
favorite foods, and you want to go
through each item in the list to see
what they are. A "for loop" helps A for loop is like a little helper that
goes through each item in your
you do this easily! list, one by one. It's like having a
robot friend who picks up each
food item and shows it to you.
For loops
While loops
Imagine a while loop as a repeat
button for a task you want to do
over and over again. It helps you
keep doing something until a
specific condition is no longer true.
A while loop works like this:
You set a condition, like "Keep eating ice cream as
long as there's ice cream left in the bowl."
You start eating ice cream (the task) while there's
ice cream in the bowl (the condition is true).
You keep eating until the bowl is empty (condition
becomes false).
Once there's no more ice cream (condition is false),
you stop eating, and the loop ends.
While loops
While loops
Practice
1. Write a program that prints the
numbers from 5 to 11 on separate
lines using a for loop..
2. Print all the elements in the list on
separate lines.
3. If the number is greater than 0 print
the number is positive if it is less than
0 print the number is negative
4. Make a simple number guessing
game
5. Write a program that prints all the
even numbers from 1 to 10.