Welcome to a sneak peek of our Python for Beginners module!
Here, we'll take a simple look at how a computer program can talk to you - it's like teaching your computer to have a small conversation. We will also look at how to remember different pieces of information easily, just like making a list of items to remember for later.
This is all done step by step, and you'll see how each line you write helps your program grow.
Let's get started with something fun and useful!
Videos & transcripts:
🖱️Interactivity is what makes computer programs and games different to books and movies. One way to make a program interactive is to let the user type in some text, and then do something based on whatever the user typed in – and that’s exactly what we’re doing in this mission.
We’ll use a new Repl for each mission, so to start coding make sure you’re logged into Replit and have created a new Repl called "Mission 2.1".
Combining the print and input commands can make a program interactive - in this case, by asking the user for their name and then greeting them. We use the input command to ask the user for their name, then the print command to greet them, like this:
Once you’ve built your program, you can run it to test it out. Try typing your name and see if the program greets you correctly.
Remember, as we build our program, we will be writing it line by line. This is the same way that all computer programs and apps are built, one line of code at a time (although sometimes we can use graphical software to make it easier).
Idea for a four-frame comic:
In programming, variables are used to refer to something that changes, just like how the winner's name varies with each category in an awards ceremony. By using variables, the computer program doesn't need to be changed every time the value inside the variable changes. Variables are extremely useful and one of the core concepts in programming.
When you update your program to use variables, it means that whoever uses the program will see a greeting for their own name. Your program has become both interactive and dynamic.
Use spaces around symbols like = and + in order to make your code more readable.
📝 A variable can hold much more than just a single name. Stay tuned to see how more complex data can be stored in variables in the next missions…
We've learned how to store simple data - such as a person's name - in variables. What about if we want to store many names, many pieces of data? Say hello to lists!
Lists (also called arrays in some programming languages) are simply a way of grouping multiple items together, so that we can refer to them as a single variable.For example, here's how we could have a list of award winners:
winners = ["Taylor Swift", "Lionel Messi"]
As you can see, there are two characteristics of lists in Python:
You can either type the data directly, as I did in the winners list above, or add them with an append() command (function), like this:
Using square brackets after an equals sign means ‘make a list.’ Using square brackets without an equals sign means ‘get an item from a list using its index, i.e. its position.’
⚠️ Warning! Computers start counting from zero, not from one like humans, so to get the first item in a list, use an index (position) of zero, like this: first_winner = winners
When you separate items in a list, put a space after each comma. Yes, this too is for better readability!
This leaked module is just a taste of how interactive and dynamic programming can be with Python.
We've touched on variables and had a glance at lists, but the full Python for Beginners course awaits with a wealth of knowledge to transform you into a true code whisperer.
Imagine the programs you could write and the solutions you could create. Join us for the full experience, and let's turn your curiosity into coding expertise!
Enroll now: EntryLevel: Python for Beginners