Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we are diving into the basics of writing simple programs in Python. Who can tell me why programming is important?
It helps us give instructions to computers to do tasks, right?
Exactly! Programming enables us to tell computers what we want them to do. Let's look at our first example—adding two numbers. This is a basic yet essential task in programming.
What do we need to start with?
We need to gather input from the user. Let's see the code for adding two numbers. Notice how we use input() to get user input and print() to output.
Can you summarize the steps?
Certainly! First, we take input, then convert it to an integer, perform the addition, and finally print the result. I like to remember it as I-P-A: Input, Process, and Output.
Now, let's explore how to check if a number is even or odd. What do you think we should look for?
We should see if it's divisible by 2!
Exactly! We will use the modulo operator %. If the result is zero when divided by 2, it's even; if not, it's odd. Let's see how this looks in Python.
How does the condition work in the code?
Great question! We use an if statement to evaluate our condition. Let’s dissect the code together.
What’s a good way to remember the syntax?
You can think of it as 'If it's zero, then that!'—meaning if the condition is true, do what follows. Remembering this can help you structure your coding logic.
Now that we understand the code, let's practice writing it ourselves. Who wants to try adding two numbers?
I'll give it a shot! I remember the steps: I-P-A.
Perfect! Go ahead.
What if I want to test it with other numbers?
That's a great approach! Testing different inputs helps ensure your program works correctly. Let's all try it out now!
Can we also check if numbers are odd?
Absolutely! After the addition, switch to checking even or odd using similar steps. Just remember our earlier discussion on conditions.
Gathering data from users and displaying results.
Basic mathematical functions such as addition.
Using if-else structures to direct program flow based on conditions.
An operator to check for evenness or oddness of numbers.
Example of adding two numbers in Python: a = int(input('Enter first number: ')); b = int(input('Enter second number: ')); sum = a + b; print('The sum is:', sum).
Example of checking if a number is even or odd: num = int(input('Enter a number: ')); if num % 2 == 0: print('Even'); else: print('Odd').
If you want to add with glee, input first, then process the key!
Once in a land of numbers, there was a wise old programmer who taught all the villagers the secret of addition and whether their numbers were even or odd.
Use I-P-A: Input-Process-Output to remember the steps of a simple program!
Remember 'C-R-' for Coding Rules: Clarity and Reusability!
Term
What does input() do in Python?
Click to flip
Term
What is the modulo operator? (%)
Click to flip
Term
What is an if statement used for?
Click to flip
Data that is entered into a program by the user.
Data that is produced by the program and displayed to the user.
Statements that control the flow of the program based on certain conditions.
An operator that returns the remainder after division of one number by another.
This section introduces simple programming in Python through practical examples, focusing on adding numbers and checking if a number is even or odd.
In this section, students learn to write simple Python programs, including how to add two numbers and determine if a number is even or odd. This illustrates the fundamentals of coding, such as inputs, outputs, and basic control structures like conditional statements.
In this section, we explore how to write simple programs using Python, focusing specifically on two practical examples: adding two numbers and checking if a number is even or odd.
This program demonstrates how to take input from the user, perform a basic arithmetic operation (addition), and then output the result. The program uses the input() function to prompt the user for two numbers, converts those inputs to integers, adds them together, and prints the sum.
# Program to add two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("The sum is:", sum)The second example illustrates the use of conditional statements in coding. The program checks if the user-provided number is even or odd by using the modulo operator. If the remainder when divided by 2 is zero, it prints 'Even'; otherwise, it prints 'Odd'.
# Program to check even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")These examples showcase the fundamental concepts of inputs and outputs in programming and introduce beginners to basic code structure in Python.