code for fun presentation
Transcript: functions loops functions are like recipes. example: when making pancakes, you don't need to remember all the steps each time. instead, you just follow the recipe. Similarly, in programming, you can use a function to perform a task instead of writing out all the steps every time. variables - example: baking cookies - take scoop of dough, place on tray until all dough is used up - loop is the process of scooping and placing dough on tray repeatedly - each iteration of the loop is like placing one cookie on tray - loop continues until dough runs out - in cs, loops allow us to perform repetitive taks effieciently for a certain number of times or until a condition is met - containers or boxes that hold different kinds of information - like jars in a kitchen: each jar holds something different (sugar, flour, spices, etc.) - can change what's stored in a variable - handy because they allow programs to work with different types of information - helps keep track of information myAge = 10 print("My age now: ", myAge) myAge = 12 print("My age in 2 years: ", myAge) - store and manipulate data easily - easily update age from 10 to 12 without changing rest of code - when we print age we don't need to remember exact value each time - code becomes easier to read and understand conditionals - like making decisions just like you do every day - example: Imagine you're deciding whether to take an umbrella when you see clouds outside. If you see clouds, you take the umbrella; if not, you leave it behind. - in computer programs, we use conditionals to make decisions based on certain conditions. - example: check to see if old enough to vote age = 20 if age >= 18: print("You are old enough to vote!") else: print("You are not old enough to vote yet.") - little tests in a computer program that help it decide what to do next based on the situation it's in - instructing the computer: "If this is true, do that; if not, do something else." - you can also check multiple conditions using "elif" the basics of computer science output output My age now: 10 My age in 2 years: 12 code multiple conditions weather = "rainy" if weather == "sunny": print("Wear sunglasses") elif weather == "rainy": print("Take an umbrella") elif weather == "snowing": print("Wear a warm coat and boots") else: print("Just wear a regular outfit") longer code longer - less readable code output: 1 2 3 4 5 input: print(1) print(2) print(3) print(4) print(5) shorter code shorter - more readable code input: for num in range(1, 6): print(num) output: 1 2 3 4 5 function w/ no argument function w/ no argument output: Hello, Grandma! code: # Define a function called greet def greet(): print("Hello, Grandma!") # Call the greet function greet() function w/ argument function w/ argument output: The square of 5 is: 25 # Define a function called square def square(number): return number * number # Call the square function with an argument result = square(5) # Print the result print("The square of 5 is:", result)