Difference between revisions of "Variables - Python"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=What is a Variable= A variable can be used to store a value within your program. Most languages require you to declare a variable and then assign a value to it, however pytho...")
 
(Now Try)
 
(17 intermediate revisions by the same user not shown)
Line 22: Line 22:
 
print("name value is " + name)
 
print("name value is " + name)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
You can perform calculations with variables:
 +
<syntaxhighlight lang=python>
 +
counter = 100 # An integer assignment
 +
miles = 1000.0 # A floating point
 +
name = "John" # A string
 +
counter = counter -1
 +
miles = 1000 * 5
 +
test = miles / 2
 +
</syntaxhighlight>
 +
 +
The arithmetic operators in python are:
 +
 +
[[File:Operators.png|600px]]
 +
 +
=Now Try=
 +
'''1)''' You run a gardening business, your main work is mowing customers lawns. Create a program to calculate the cost if:
 +
* The lawn is 8 metres by 12 metres
 +
* You charge £1.75 per square metre
 +
* To pay for the up keep of your tools you add 5% to every job
 +
 +
'''2)''' If a pay as you go mobile phone charges:
 +
* 10p per text message
 +
* 35p per minute for voice calls
 +
* 15p per MegaByte of data
 +
 +
How much will it cost if you made:
 +
* made 150 minutes of voice calls
 +
* sent 250 text messages
 +
* used 200 MegaBytes of data
 +
 +
==Extension Tasks==
 +
*Create a program to calculate the area of a circle
 +
*Create a program to calculate the volume of a cylinder
 +
*Create a program to use Pythagoras theorem

Latest revision as of 10:39, 4 March 2019

What is a Variable

A variable can be used to store a value within your program. Most languages require you to declare a variable and then assign a value to it, however python does not.

In most languages, a variable is of a specific data type and this can't change during the program. However python will determine the data type to use from the data stored in the variable.

Variables in Python

You need to give a name for the variable and then assign it a value:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string

You could print the current values for each variable:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string 
print("counter value is " + counter)
print("miles value is " + miles)
print("name value is " + name)

You can perform calculations with variables:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string 
counter = counter -1
miles = 1000 * 5
test = miles / 2

The arithmetic operators in python are:

Operators.png

Now Try

1) You run a gardening business, your main work is mowing customers lawns. Create a program to calculate the cost if:

  • The lawn is 8 metres by 12 metres
  • You charge £1.75 per square metre
  • To pay for the up keep of your tools you add 5% to every job

2) If a pay as you go mobile phone charges:

  • 10p per text message
  • 35p per minute for voice calls
  • 15p per MegaByte of data

How much will it cost if you made:

  • made 150 minutes of voice calls
  • sent 250 text messages
  • used 200 MegaBytes of data

Extension Tasks

  • Create a program to calculate the area of a circle
  • Create a program to calculate the volume of a cylinder
  • Create a program to use Pythagoras theorem