Assignment of Variables A simple Python program is a sequence of instructions, written one per line, and executed one by one from top to bottom. Unlike Basic you do not need numbers for each line. An assignment is performed via the 'equals sign' in a line of code that 'assigns' a value to a variable. A comma is used to separate two things to be printed, in the same way we use commas in English to enumerate two, three, or more things; text is written between 'quotes', which are not printed themselves. In Python, a sequence of characters surrounded by 'quotes' is called a string. Numeric variablesFor example:
The word 'people' is the name of the variable. We have assigned the value '7' to people so the computer stores the value of the variable 'people' as '7' That is very simple but the computer can cope with complicated values - ones given as an equation. Let's assign the variable a value expressed as a mathematical expression. The computer then has to evaluate the expression to the right of the assignment (=) and store the result as the value of the variable on the left of the assignment. For example:
Here the BODMAS rule is applied and
Brackets first: pets = 4**2*3/2+14-12 Then 'Order' - numbers raised to a power: pets = 16*3/2+14-12 Now Division: pets = 16*1.5+14-12 Then Multiplication: pets = 24+14-12 Then Addition pets = 38-12 and finally Subtraction pets = 26 Here is an interactive python program - hosted by Trinket. I came across this site via the Open University - and I figured if they used it, it would be safe and as they endorsed it why shouldn't I! Press the 'play' button to execute our little program. Note the additional bit of coding involving 'print' - enabling us to see the result of our assignment exercise. See here to read about the types of number variables you can have in python. String variablesCharacters or sets or characters (text and/or numbers that are not used with their numeric value) are enclosed in single or double quotes to make them a string variable value. eg.
or
It does not matter to Python which 'quote symbol' you choose to use - but you have to stick to the same type at the beginning as at the end of the variable string. In Java, you must use double quotes for strings, and are only allowed to use single quotes for characters. That's not the case with Python - you can use either. But it may be a good idea to develop the habit of using double quotes.... The Problem with Quotation Marks inside a StringThe English language is full of single quotation marks (apostrophes). For example, we can write: they are or they're However the second option is problematic if you want to use single quotes to surround strings in Python: print ('They're going skiing this winter.') This coding would cause an error because Python thinks the string ends after They, so everything after it is considered a syntactical error. There are ways around it: Stop using contractions (they are NOT they're) - Not practical at all. Use double quotation - print ("They're going skiing this winter.") However, if you want to print a speech quotation. You need to do the opposite! You will soon learn how to adapt.... Probably the best way to avoid the problem is to use a 'backslash' before the quote you want to be recognised as part of the text string, not part of the coding... this also makes you carefully think about quotes when included in your code writing. print ('They're going skiing this winter.') will become print ('They\'re going skiing this winter.') print ("He said, "Hello"") will become print ("He said, \"Hello\"") |
|