• Welcome to Python Sequencing Puzzles Part 1 Python Conventions Sequencing Puzzles Part 2 Review and Quiz Unit 1 Project

Python Conventions

Learn more about Python syntax and Python coding conventions.

In this lesson, you will learn the following:

  • How to use comments
  • Naming conventions
Next
© Tynker, 2020

Comments

A comment is a human-readable description of the instructions in a program.

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.

Run the code below to see how comments are ignored by the computer.

In this example you can see how comments can help explain code to a different programmer. Even if this is the first time you had seen this puzzle, the comments explain what the functions are doing.

Multi Line Comments

You can also create multiple line comments that will allow you to explain your code over multiple lines. To do this, use triple quotation marks """ The same rules apply, the computer will not try and perform the tasks given inside of the comment block. Comments can be useful for debugging as well.

Previous Next
© Tynker, 2020

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.

Functions are Lowercase

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.

Use snake_case for Long Names

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.

Fix This Code!

These variables are not following the snake_case naming convention. Try to fix this code so it follows the naming rules above.

Reserved Words

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.

Fix This Code!

You can't use def as a variable name because def is a reserved word.

Give the variable a different name, then print the variable on the next line.

Some common reserved words and their uses are:

  • def: Declare a function
  • for: Create "for" loops
  • if: Create "if" statements
  • else: Create "if-else" statements
  • while: Create "while" loops
  • break: Break out of loops
  • class: Declare a class definition

Do It Yourself

Fix the code below so that it follows the naming rules we described in this lesson. Make sure you follow snake_case naming convention and rename any variables that are using reserved words!

Previous Next
© Tynker, 2020

Review

Let's review what you've learned so far.

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.

Comments

A comment is a human-readable description of the instructions in a program.

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.

Previous Done
© Tynker, 2020
Sign in
Don't have an account?