[Codecademy] PYTHON SYNTAX

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 %.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,813评论 0 23
  • 我已经不记得父亲对我说的最后一句话是什么了。 最后一面的前一个镜头,是我坐在公共汽车上驶向远方,父亲站在合欢树下驻...
    禾末阅读 958评论 0 0
  • 苏菲的世界从两个问题开始:你是谁?世界从何而来? 成为一个优秀哲学家的唯一条件是要有好奇心。 从神话的世界观开始,...
    Stonedahan阅读 209评论 0 0
  • 简介 沈从文笔下的《龙朱》,犹如看见了一幅画,画中山花烂漫、草长莺飞,漫步其中,如同经历了一个清新怡人的梦境。这是...
    顽皮的panda阅读 5,621评论 0 2
  • 2011-01-08 00:34:26 (上) 十二月的月亮,酒红色摇摇晃晃 爱是个拳头 赐你黑眼圈一双 你把嘴唇...
    四两金阅读 255评论 0 1