Learning about variables is going to be very important as you progress with programming in python. Think of a variable as a storage box for a value. e.g. x = 3. x is the variable and it holds the value 3. In this session we will be learning about the different data types (such as integer, string,boolean, float etc.) and how to declare them. We will create some simple programs that require variable declaration and working with some simple calculations.
Download Python 3 here:
1.First, run the code to see how it works!. We have declared a number of different variables, of different types (string, integer, float, (decimal number) and finally Boolean (True or False)
2. Add your own name, age, pocketmoney and whether you love programming to the variables a,b,c, and Loves_Programming. Print them out to the screen.
def main(): x="Joe" #x is a string variable that holds a user's name y=14 #y is a float variable that holds the user's age z=10.50 #z is a variable that holds the user's pocket money balance InSchool=True #InSchool is a boolean variable. Boolean variables can be TRUE or FALSE (they can hold one of two values) print(x) print(y) print(z) print(InSchool) a="Your Name" b=0 c=0 Loves_Programming=False main()
1.Run the code below and note that we have declared two simple integer variables x and y, with the values 3 and 2.
2. Add a print command to print the variables y+y. What will the output be?
3. Declare another variable called z. Give z the value 4. Add a print command to add up all the variables x,y,and z and print it to the screen.
def main(): x=3 y=2 print(x+y) print(x+x) main()
1. The program below has three functions, one for addition, one for subtraction, and one half finished one for multiplication.
2. Run the program and analyse how it works. Then see if you can use the previous functions to fill in the multiply function and get it to work. It should multiply x and y and store the result in variable z. (as it was done in the add and subtract functions) Print the result to the screen.
3. See if you can create another function for division. Divide x / y (use the / operator to do this) and store the result of the division in variable z. Print z to the screen
def add(): print("This is a cool program that will add variables x and y up for you") x=4 y=2 z=x+y print(z) subtract(x,y)#this is calling the subtract function, and passing it the values of x and y def subtract(x,y): print("....and this program will subtract for you") z=x-y print(z) multiply(x,y) def multiply(x,y): print("This part of the program will multiply x and y") #Your code here. add()
1.Boolean variables are strange in that they can only contain the value True or False. An example is shown in the program below. The code for a cinema program contains seats. The seats are set to either True or False depending on whether they are booked or available. If they are booked, then they are TRUE. If they are available, then seatbooked=False.
2. When you play the code you'll notice that it prints TRUE for all Seats 1 - 4, but the boolean variables for seat 2 and seat 3 have been set to False. How can you fix this?
def main(): #Set Seat to True if booked, False if available seat1booked=True seat2booked=False seat3booked=False seat4booked=True print("Seat 1 is booked?:",seat1booked) print("Seat 2 is booked?:",seat1booked) print("Seat 3 is booked?:",seat1booked) print("Seat 4 is booked?:",seat1booked) main()
1.Run the code below and note that it generates a username based on the variables name and age. In this case 33 is in speechmarks, which makes it a text(string) variable and not integer
2.Replace the first name and age variables with your own name and age, to create a username
3. What would happen if you declared the age variable as a integer: age=33 instead of age="33"? Try it and note the result! Why do you think this happens?
def main(): firstname="Joe" age="33" print("Your username could be:"+ firstname+age) main()
Use your knowledge and skills from the completed tasks. Answer as many of the following questions as you can. Feel free to use the internet for your answers.
Note: You can print the page, screenshot, or write down the answers in your notebook
def main(): print("Here are your test scores - each one was out of /10") name="Joe Bloggs" test1=9 test2=5 test3=2 print("Welcome",name) print("Test1:",test1) print("Test2:",test2) print("Test3:",test3) print("Your tests added together give a total of:", test1+test1+test3) main()