a byte of A Byte of Python

Python in one picture

First Steps

Open the terminal and open the Python prompt. You should see >>> where you can start typing stuff. This is called the Python interpreter prompt.

Quit the Interpreter Prompt:

1
2
[ctrl + d] or entering exit()       # GNU/Linux or OS X
[ctrl + z] and press [enter] key # Windows

Comments

1
# This is a comment line.

Literal Constants

Numbers

1
2
3
2       # a whole number
3.14 # floating point number
2E10 # The E notation indicates powers of 10, 2E10 means 2 * 10^10

Strings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'This is a string'    # Single Quote
"This's a string" # Double Quote

# You can specify multi-line strings using triple quotes-(`"""` or `'''`). An example is:
'''This is a multi-line string. First line.
Second line.
'''

# If you want to construct strings from other information. `format()` method is useful.
age = 23
name = 'Rainy'

print('My name is {0}, {1} years old'.format(name, age))

print('My name is' + name + ', ' + str(age) + ' years old') # We could have achieved the same using concatenation.

# There can be more detailed specifications such as:
# decimal (.) precision of 3 for float '0.333'
print('{0:.3f}'.format(1.0/3))

# fill with underscores (_) with the text centered
# (^) to 20 width '*******hello********'
print('{0:*^20}'.format('hello'))

# keyword-based 'Rainy wrote a byte of A Byte of Python'
print('{name} wrote {post}'.format(name='Rainy', book='a byte of A Byte of Python'))

Since we are discussing formatting, note that print always ends with an invisible “new line” character (\n) so that repeated calls to print will all print on a separate line each. To prevent this newline character from being printed, you can specify that it should end with a blank:

1
2
3
4
print('a', end="")
print('b', end="")
print('c', end=" ")
print('d', end="*")

Output is:

1
abc d*

Escape Sequences:

1
2
3
4
5
6
7
8
9
print('What\'s your name?') # Output is: What's your name?

print('This is the first line\nThis is the second line') # Output is:
# This is the first line
# This is the second line

print("This is the first sentence. \
This is the second sentence.") # Output is:
# This is the first sentence. This is the second sentence.

If you need to specify some strings where no special processing such as escape sequences are handled.

1
print(r"Newlilnes are indicated by \n")

Always use raw strings when dealing with regular expressions.

Variable

1
2
_Rainy Rainy rainy # valid identifier
2BRainy >Rainy Rainy-Python # invalid identifier

Date Types

The basic types are numbers and strings.

Object

Python refers to anything used in a program as an object.

Logical And Physical Line

Python implicitly assumes that each physical line corresponds to a logical line.

1
2
i = 5
print(i)

is effectively same as

1
i = 5; print(i)

However, I strongly recommend that you stick to writing a maximum of a single logical line on each single physical line.

1
2
3
4
s = "I love you. \
I need you.'
print(s) # Output is:
# I love you. I need you.

Sometimes, there is an implicit assumption where you don’t need to use a backslash. This is the case where the logical line has a starting parentheses, starting square brackets or a starting curly braces but not an ending one. This is called implicit line joining.

Indentation

Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.

1
2
3
4
i = 5
# Error below! Notice a single space at the start of the line
print('Value is', i)
print('I repeat, the value is', i)

Python will always use indentation for blocks and will never use braces.

本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明出处! © 雨落
  1. 1. First Steps
  2. 2. Comments
  3. 3. Literal Constants
    1. 3.1. Numbers
    2. 3.2. Strings
  4. 4. Variable
  5. 5. Date Types
  6. 6. Object
  7. 7. Logical And Physical Line
  8. 8. Indentation