Open xie186 opened 3 years ago
Variables are a way of storing pieces of data in your program. When you create a variable, an area of the computer’s memory is reserved for you and the data is stored in it. This area of memory is now controlled by you: you can retrieve data from it, change the data, or get rid of it all together.
To create a variable in Python you give it a name and make it equal to a value. This is known as assignment. For example:
my_variable = "some useful data"
Next you will change your program to store your name in a variable and then display it.
Add the following code to the bottom of your program to create a new variable called my_name to hold your name
my_name = "Martin"
When your computer runs this instruction, a variable called my_name will be created and the text of your name will be stored.
Once a variable has been created, it can then be used in your program.
Use print to output your name to the screen by adding this code to the bottom of your program
print(my_name)
Notice how when using print you put the name of the variable between the () instead of text. The print instruction will retrieve the value from the my_name variable and display it.
Run your program to see your name appear under the first two print statements
Tip: A common problem is to put the variable name inside speech marks, e.g. print("my_name"). (What will this do? If you are not sure, try it!)
You can improve the message by putting the text “My name is “ in front of your name.
Modify the code to add the text "My name is " before the my_name variable:
print("My name is " + my_name)
Note the use of the + sign to add the first piece of text to the text in the variable. Adding pieces of text together like this is known as concatenation.
https://www.futurelearn.com/courses/programming-101/
Python To create Python programs, you need a text editor to write your code, and a Python interpreter. You write the code into the text editor and the interpreter executes your code on the computer.
An editor, interpreter, and other useful tools (e.g. a file browser) are often bundled together into an integrated development environment (IDE). IDEs make creating programs much easier.
You will be using an IDE to create, run and test your Python programs. You can install an IDE on your computer, or you can use an internet browser to access an online IDE. An installed IDE has the benefit of being able to work when you are not connected to the internet. On the other hand, an online editor doesn’t require anything to be installed.
Instructions are provided below for an installed IDE called Mu and for the online IDE Trinket.