Learning to Code with Python: Lesson 1.3 - Variables
Skills:
Text Coding for Kids90%
Key Takeaways
Declares and uses variables in Python programming
Full Transcript
Hi, welcome to learning to code with Python. Lesson three, variables. Variables are a really important concept in programming. A variable's like a label. It's a shortcut for pointing at something that we want to save. Here are some examples. So, I've set apples equal to 10. So, now if I say print apples, I see that apples now is equal to the number 10. If I want to change what apples points to, I can say, now make apples equal to 15. You can also store other things than numbers in a variable. For example, variables can be named just about anything you can think of. It's usually a good idea to pick a name that makes it clear what the value means. For example, if we were making a game where your goal was to collect flowers, that might be a good name for the variable, like this. If you name it bubbles, then later when you want to print out the score, you're going to be confused by why the number of flowers you've collected is called bubbles. So, now that we know what variables are, let's talk about what kinds of things we can store in them. Information inside the computer is called data. And there are lots of different types of data that the computer can remember. Let's start with numbers. We've used those a few times already. Did you know you can use the Python shell like a calculator? Try this out. We can add numbers. We can subtract numbers. We can multiply. And that asterisk there is the symbol for multiplication. It's the number above the eight on your keyboard. So, press shift and eight to get it. And last, you can, of course, divide. You can also calculate with variables. You remember we have our apples variable? Well, apples * 2 is 30. Another type of data we can work with is text. Remember our first program? We printed Hello, world to the screen. The stuff inside the quotation marks is called a string. A string just means a bunch of characters. Those characters can be letters, numbers, whatever we can type. We put them inside quotes so the computer knows that they're not commands. Here's an example. See, I put the word orange in quotes. So, now fruit is equal to orange. What if I left the quotes out? I get an error message, right? Because it thinks orange is a variable. So, orange should be pointing at something, but we never told orange to point at anything. So, the computer doesn't know what we mean. Let's see a little more about how strings work. Strings can be added together. See how adding those two strings together just mushed them together? I didn't include a space, so the computer didn't put one there, either. In fact, every string is just a bunch of individual characters added together. That's why they're called strings. We'll work more with strings later in the class. All right, I've reset my window just so it won't be so cluttered. You don't have to do that if you don't want to. So, the last type of data I'd like to talk about is a list. Lists are really useful if you need to keep track of a bunch of things. This is what a list looks like. We use a square bracket and then any number of items can go inside it separated by commas. You can also make lists that are lists of strings. So, for example, my friends are Bob, Sue, and Alex. And that's a list. Now, how would we use these? Let's imagine that we're building a game about wizards and the player has to collect a bunch of items to make a magic potion. With a list, we can keep track of what's in their backpack. And let's say they've collected a bat eye, some slug slime, and a lizard gizzard. Okay? And that's their list of items inside their backpack. Now, when the computer has items in a list, it keeps track of them by numbering them. However, when a computer counts, it counts a little differently from how you or I might do it. It always starts counting at the number zero. These numbers are how it keeps track of what place in the list each item is, and that's called an index. Let's see how we would use indexes. Backpack number zero is the first item in the list. Backpack number one, item number one is the slug slime. But what if we say we want backpack item number three? Well, we get an error message, right? It says the index is out of range. That's because the list only has an item number zero, an item number one, and an item number two. There is no item number three. It takes a little getting used to to remember to start counting with zero, but with practice, you'll get used to it in no time. Now, lists are useful because if we want to change something, we don't have to retype the whole list. Let's say that instead of slug slime, the wizard actually has red slug slime. Well, we can just change that one item. I want to change backpack item number one to be red slug slime. Now, if I print out the whole backpack, only that item has changed. We didn't have to worry about changing any of the other items in the list.
Original Description
Learning to Code with Python
Lesson 1.3: Variables
Support me on Patreon: http://patreon.com/kidscancode
Links:
* Installing Python: http://kidscancode.org/python-install.html
* Asking for help: help@kidscancode.org
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from KidsCanCode · KidsCanCode · 3 of 60
1
2
▶
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
More on: Text Coding for Kids
View skill →Related Reads
📰
📰
📰
📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Medium · Programming
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Dev.to · Ogunkola Adeola
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Dev.to · born1987-ir
How Kids Can Build Fighting Games Stickman in Scratch
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI