A brief note on Getting Started with Python: print() and comments, reading an error message, variables and the four types, good names, and Python as a calculator with //, % and **. With a worked example, practice, a program and questions to check yourself.
A brief note on Getting Started with Python. Read it before a test. Try each question at the end before you open its answer.
print() shows things on the screen. Text goes inside quotation marks: print("Hello").print(3 + 4) shows 7.sep changes the space, and end changes what comes after.# is a comment. Python skips it; it is there for the person who reads the program.SyntaxError means Python cannot read the line, often a missing bracket or quotation mark.NameError means a name Python does not know: a spelling mistake, a wrong capital letter, or a variable used before it was made.marks = 72. The value on the right goes into the name on the left.score = score + 10 works out the right side first. += is the short form.| Type | Holds | Example |
|---|---|---|
int | a whole number | 42 |
float | a decimal number | 3.5 |
str | text | "Asha" |
bool | true or false | True |
type() tells you the type. int(), float() and str() change a value from one type to another.+ adds numbers but joins text, so "4" + "5" is "45". You cannot join text and a number.total_marks.| Sign | What it does | Example | Gives |
|---|---|---|---|
+ - * | add, take away, multiply | 7 * 3 | 21 |
/ | divide, always a decimal | 7 / 2 | 3.5 |
// | the whole number of times | 7 // 2 | 3 |
% | the remainder | 7 % 2 | 1 |
** | a power | 2 ** 5 | 32 |
* / // %, then + -. When you are not sure, use brackets.quotient * divisor + remainder gives back the number you started with.round(x, 2) rounds to 2 places. int(x) cuts the decimal part off. abs(), min(), max() and divmod() are ready to use.What does this program show?
Follow it one line at a time, with a trace table.
| Line | x | y |
|---|---|---|
x = 7 | 7 | |
y = x * 2 | 7 | 14 |
x = x + 1 | 8 | 14 |
y = y - x | 8 | 6 |
x at that moment, 7, so y is 14.x to 8. The value of y does not change: it holds 14, not the rule "twice x".14 - 8, so y becomes 6.The program shows 8 6.
A farm packs its eggs into boxes of 12. The program finds the full boxes, the eggs left over and the money for the full boxes.
Print("Hi") stops with NameError: name 'Print' is not defined. Python reads print and Print as different names.print("Age: " + 12) stops with TypeError: can only concatenate str (not "int") to str. Use a comma, or str(12)./ for a whole number. / always gives a float: 8 / 4 is 2.0. Use // for a whole number of times.int() for rounding. int(4.9) is 4, because it cuts off. round(4.9) is 5.print or sum. The name now holds your value, and the tool stops working.price = 40, then total = price * 3, then price = 50. What is total at the end?2nd_place, total_marks, my-score, class?7, 7.0, "7" and 7 > 3.-3 ** 2 in Python and gets -9, but her calculator gives 9 for minus 3 squared. Why, and how should she write it?Log in to join the discussion.
Loading…