Course 1.2Lesson 4 of 5
A name can have letters, digits and underscores, but it cannot start with a digit, contain a space or a hyphen, or be a Python keyword. Small and capital letters are different, so score and Score are two variables. A good name says what the variable holds, and the usual style is small letters with underscores, as in total_marks. Never give a variable the name of a function that Python already has, such as str or print. A comment, which starts with #, explains the program to a person.
Try the code and the simulations beside the lesson.
After this lesson you can
There are two kinds of rule about names.
A name
_,2nd_place is not allowed and place_2 is,my age and my-age are not allowed (Python reads the hyphen as a minus sign), and my_age is,if, for, while, class or True,score and Score are two different names.Here is the full list of the keywords.
Two names that differ only by a capital letter are easy to mix up. Python allows them, but good programs avoid them.
Python has many functions that are always ready: print, str, int, float, len, sum, max, min, list, type. They are not keywords, so Python allows you to use their names for your own variables. But then your variable hides the function, and it stops working. Watch this.
The rule: never use the name of a function that Python has. If you want a name for a word, choose word or text. For a total, choose total and not sum. For the biggest number, choose highest and not max.
A name should say what the variable holds.
A line that starts with # is a comment. Python ignores it. It is a note from you to a person who reads the program. Use a comment to explain why, or what a step does. Do not repeat the code in words.
x = x + 1 # add 1 to xvisits += 1 # one more visitor has arrivedThe programs in this course start each step with a comment. Read them as you read a recipe.
1st_prize. Write first_prize.my-age or my age. Write my_age.class = "7B" or for = 3. Write class_name, count.str, sum, max, list. Choose another word.Total and total.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
a, b, c), and ask a friend to say what the program does. Then change them back, and ask again.score and Score are different names.str, print, sum and other function names for your variables.# and explains the program to a person.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
total_1, 1_total, my total, my_total, for.sum = 0 a risky line?Age = 12 followed by print(age) correct?x = 98.6, if it is a body temperature.Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.