Abstract from https://docs.python.org/3/tutorial/introduction.html
Numbers
Comments in Python start with the hash character,
The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators
Numbers
Comments in Python start with the hash character,
#, and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators
+, -, * and / work just like in most other languages (for example, Pascal or C); parentheses (()) can be used for grouping.The integer numbers (e.g.
2, 4, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float. We will see more about numeric types later in the tutorial.Division (
/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %
With Python, it is possible to use the ** operator to calculate powers
StringsStrings can be enclosed in single quotes ('...') or double quotes ("...") with the same result [2]. \ can be used to escape quotes
If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote.print(r'C:\some\name')
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line.
Strings can be concatenated (glued together) with the + operator, and repeated with *.
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one.Indices may also be negative numbers, to start counting from the right.Note that since -0 is the same as 0, negative indices start from -1.In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
Attempting to use an index that is too large will result in an error.
The built-in function
Lists
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type
Like strings (and all other built-in sequence type), lists can be indexed and sliced.
Lists also support operations like concatenation.
Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their conten.You can also add new items at the end of the list, by using the
Assignment to slices is also possible, and this can even change the size of the list or clear it entirely,
Conditionals
The built-in function
Multi Assignments
In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false.
The standard comparison operators are written the same as in C:
The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line.
The
len() returns the length of a string.Lists
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type
Like strings (and all other built-in sequence type), lists can be indexed and sliced.
Lists also support operations like concatenation.
Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their conten.You can also add new items at the end of the list, by using the
append() method (we will see more about methods later).Assignment to slices is also possible, and this can even change the size of the list or clear it entirely,
Conditionals
The built-in function
len() also applies to list.It is possible to nest lists (create lists containing other lists).Multi Assignments
In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false.
The standard comparison operators are written the same as in C:
< (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to).The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line.
The
print() function writes the value of the argument(s) it is given. It differs from just writing the expression you want to write (as we did earlier in the calculator examples) in the way it handles multiple arguments, floating point quantities, and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely,The keyword argument end can be used to avoid the newline after the output, or end the output with a different string.
Comments
Post a Comment