Functions in Python · Building a Calculator · C++ basics with #include & iostream · Variables & Strings
Use the arrow keys, the dots below, or swipe to move through the deck →
Packaging up a piece of work so you can run it by name, as many times as you like.
A function is a named, reusable block of code that performs a task. Instead of retyping the same steps every time, you write them once and call the function whenever you need them.
defreturndef greet(name): print("Hello, " + name + "!") # calling the function greet("Prince") greet("Maria")
def add(a, b): return a + b
def tells Python "a function starts here"add — how you'll call it latera, b — the inputs it expectsCalling add(2, 5) runs the body with a = 2 and b = 5, then hands back 7.
def welcome(): print("Welcome!")
def shout(msg): print(msg.upper())
def square(n): return n * n
def power(n, exp=2): return n ** exp
Putting functions to work in one small, complete program.
input(), then convert the text to numbers with float().+, -, *, or /.while loop so it runs again until the user quits.def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def divide(a, b): if b == 0: return "Error: cannot divide by zero" return a / b x = float(input("First number: ")) op = input("Operation (+ - * /): ") y = float(input("Second number: ")) if op == "+": result = add(x, y) elif op == "-": result = subtract(x, y) elif op == "*": result = multiply(x, y) elif op == "/": result = divide(x, y) else: result = "Unknown operation" print("Result:", result)
input() always returns text (a string), even if the user types a number.float() converts that text into a real number you can do math with.if / elif / else picks exactly one branch based on the symbol typed.First number: 12 Operation (+ - * /): * Second number: 4 Result: 48.0
#include and iostream — how a C++ program gets its tools.
#include do?#include is a preprocessor directive — an instruction handled before your code is even compiled. It copies in code from another file so you can use tools you didn't write yourself.
#, no semicolon at the end<iostream> mean "look in the standard library""myfile.h" mean "look in my own project files"iostream = input/output stream — gives you cin and cout#include <iostream> using namespace std; int main() { cout << "Hello, world!"; return 0; }
#include <iostream> using namespace std; int main() { cout << "Hi!"; return 0; }
cout instead of std::coutcin >> works the opposite way — it reads input from the keyboard, e.g. cin >> age;
The basic containers every program is built from.
A variable is a named box that holds a value in memory. You give it a name once, and use that name to read or change the value later.
name = valueint age = 20;score, not x# Python age = 20 name = "Prince" is_student = True
// C++ int age = 20; string name = "Prince"; bool isStudent = true;
A string is text — any sequence of characters wrapped in quotes. Used for names, messages, anything that isn't pure numbers to calculate with.
'hi' or "hi"+, called concatenationname[0]#include <string> and the type string# Python first = "Prince" last = "Reyes" full = first + " " + last print(full) # Prince Reyes print(full[0]) # P print(len(full)) # 12
Reusable blocks defined with def, called by name, can take input and return output.
Small functions + input() + if/elif/else = a complete working program.
Pulls in the input/output toolkit so C++ can use cin and cout.
Named boxes for values; strings are text you can join, index, and measure.