forked from kavyanaik376/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasics of Python
More file actions
122 lines (111 loc) · 4.41 KB
/
Basics of Python
File metadata and controls
122 lines (111 loc) · 4.41 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
* Variables
One of the most powerful features of a programming language is the ability to
manipulate variables. A variable is a name that refers to a value.
An assignment statement creates new variables and gives them values:
>>> message = 'And now for something completely different'
>>> n = 17
>>> pi = 3.1415926535897931
This example makes three assignments. The first assigns a string to a new variable
named message; the second assigns the integer 17 to n; the third assigns the
(approximate) value of π to pi.
To display the value of a variable, you can use a print statement:
>>> print(n)
17
>>> print(pi)
3.141592653589793
The type of a variable is the type of the value it refers to.
>>> type(message)
<class 'str'>
>>> type(n)
<class 'int'>
>>> type(pi)
<class 'float'>
* Variable names and keywords
Programmers generally choose names for their variables that are meaningful and
document what the variable is used for.
Variable names can be arbitrarily long. They can contain both letters and numbers,
but they cannot start with a number. It is legal to use uppercase letters, but it is
a good idea to begin variable names with a lowercase letter (you’ll see why later).
The underscore character ( _ ) can appear in a name. It is often used in names with
multiple words, such as my_name or airspeed_of_unladen_swallow. Variable
names can start with an underscore character, but we generally avoid doing this
unless we are writing library code for others to use.
If you give a variable an illegal name, you get a syntax error:
>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax
76trombones is illegal because it begins with a number. more@ is illegal because
it contains an illegal character, @. But what’s wrong with class?
It turns out that class is one of Python’s keywords. The interpreter uses keywords
to recognize the structure of the program, and they cannot be used as variable
names.
Python reserves 35 keywords:
and del from None True
as elif global nonlocal try
assert else if not while
break except import or with
class False in pass yield
continue finally is raise async
def for lambda return await
You might want to keep this list handy. If the interpreter complains about one of
your variable names and you don’t know why, see if it is on this list.
* Statements
A statement is a unit of code that the Python interpreter can execute. We have
seen two kinds of statements: print being an expression statement and assignment.
When you type a statement in interactive mode, the interpreter executes it and
displays the result, if there is one.A script usually contains a sequence of statements. If there is more than one
statement, the results appear one at a time as the statements execute.
For example, the script
print(1)
x = 2
print(x)
produces the output
1
2
The assignment statement produces no output.
* Operators and operands
Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands.
The operators +, -, *, /, and ** perform addition, subtraction, multiplication,
division, and exponentiation, as in the following examples:
20+32
hour-1
hour*60+minute
minute/60
5**2
(5+9)*(15-7)
There has been a change in the division operator between Python 2.x and Python
3.x. In Python 3.x, the result of this division is a floating point result:
>>> minute = 59
>>> minute/60
0.9833333333333333
The division operator in Python 2.0 would divide two integers and truncate the
result to an integer:
>>> minute = 59
>>> minute/60
0
To obtain the same answer in Python 3.0 use floored ( // integer) division.>>> minute = 59
>>> minute//60
0
In Python 3.0 integer division functions much more as you would expect if you
entered the expression on a calculator.
* Expressions
An expression is a combination of values, variables, and operators. A value all by
itself is considered an expression, and so is a variable, so the following are all legal
expressions (assuming that the variable x has been assigned a value):
17
x
x + 17
If you type an expression in interactive mode, the interpreter evaluates it and
displays the result:
>>> 1 + 1
2
But in a script, an expression all by itself doesn’t do anything! This is a common
source of confusion for beginners.
Exercise 1: Type the following statements in the Python interpreter to
see what they do:
5
x = 5
x + 1