[Codecademy] PYTHON SYNTAX

2017-10-22  本文已影响0人  灯盏细辛

Variables

A variable stores a piece of data, and gives it a specific name.

For example:

spam = 5

Booleans

A boolean can only have two values, true or false.

a = True
b = False

Remember to capitalize!

Reassigning

To change the value of a variable.

my_int = 7                                                my_int = 3

Whitespace

Whitespace is used to structure code.

We should use two-space or four-space indentations.

Single Line Comments

We use the # sign for comments. A comment is a line of text that Python won't try to run as code. They make our program easier to understand.

# I love python!

Multi-Line Comments

The # sign will only comment out a single line. For multi-line comments, we can include the whole block in a set of triple quotation marks.

""" Python is an interesting language to study! """

Math

We can add, subtract, multiply, divide numbers like this:

addition = 72 + 23
subtraction = 108 - 204
multiplication = 108 * 0.5
division = 108 / 9

Exponentiation

We use ** to work with exponents.

eight = 2 ** 3

Modulo

Modulo returns the remainder from a division.

one = 3 % 2

Remember that we can't divide by 0 or we will get an error. Same goes for %.

上一篇 下一篇

猜你喜欢

热点阅读