Coding Tynker
Coding Tynker
Coding Tynker
Did you notice how all the commands you've written so far are all lower-case letters?
When you are writing your own Python code, variable and function names always start with lowercase
letters.
color()
print()
forward()
If a variable or function name has several words, you'll still want to make everything lowercase. And
instead of using spaces, separate words with underscores.
game_over
the_largest_one
get_first_name()
get_last_name()
^.^
Let's learn one more tricky rule for naming your own variables and functions. You cannot use reserved
words.
Reserved words are special commands that are built into the Python language. Because they already
have special meanings to Python, you can't use them to name things.
for
else
if
Some common reserved words and their uses are:
Syntax
Just like with English, Python has its own grammar called syntax. Code that does not follow Python's
syntax will produce a syntax error.
Spacing
Spaces between values and in function calls does not not affect how the computer interprets the
instruction.
print("hello")
print ( "hello" )
print ( "hello" )
Comments
Good programmers add comments to make their code easier to understand by other programmers. In
Python, comments begin at the # sign - everything after the # is ignored by the computer.
Naming Conventions
You've probably already seen how careful you need to be when you're writing Python code. If you
misspell one word or forget a parenthesis, your code won't run at all!
Programmers call these rules naming conventions. Using these conventions will make your code run
without errors—and keep your code readable, too.
Review
Let's review what you've learned so far. Read carefully for the Quiz coming up next!
Syntax
We call the rules for writing Python code syntax. If you don't follow the rules, your Python code won't
run properly.
Function Calls
The commands we have used so far are function calls. For example, the forward() command is making a
function call to the function named forward.
forward() Valid
forward(); Valid
forwardInvalid
forward; Invalid
forward( Invalid
forwardInvalid
Arguments
When a value is used in a function call, it is called an argument. Arguments are enclosed in parentheses
after the function name.
print("...")
Variable and function names start with lowercase letters. For example: color, move, forward.
Snake Case
If a variable or function name has several words, all words will be lowercase and separated by
underscores. For example: game_over, high_score, first_name, last_name. This naming convention helps
with understanding and debugging code.
When you write your own functions, variables, or other identifiers, you need to follow some rules for
naming them. Here's a quick summary of Python's naming conventions for identifiers:
Identifiers cannot have spaces in them.
Identifiers must begin with a letter, or an underscore _. They can't begin with numbers!
Identifiers cannot contain symbols such as exclamation marks (!), hyphens (-), periods (.), commas (,),
and so forth.
You can use uppercase letters, but Python doesn't treat uppercase and lowercase letters the same. In
other words, jump, JUMP, and jUmP are not the same identifier to Python!
Syntax Valid?
oneforward() Valid
forward1() Valid
forward!!() Invalid because function names cannot have symbols such as exclamation points
ForWarD() Technically valid, but doesn't follow Python naming conventions (the function should be
all lower case)
Comments
Commenting is a way to write an explanation of your code, right within the code itself. You can write
notes to yourself in your code, and it can also help other programmers understand your code if you're
working together on a project.
"""
Multi
Line
Comment
"""
In the last puzzle, you had to write the forward() command over and over again. That was kind of
annoying, wasn't it? There's an easier way to repeat code!
We'll use something called loops to run the same code many times. To start things off, let's learn about a
kind of loop called the for loop.
For Loops
For loops execute the same code a certain number of times. For loops are very useful when you need to
count (for example, count from 1 to 100) or whenever you know exactly how many times you want your
code to run.
for i in range(number):
#commands
In our code above, the letter i represents a counter variable. You can call your counter variable whatever
you like, but single letters like i and j are commonly used. These variables keep track of how many times
the program has looped.
Previous Next