Escolar Documentos
Profissional Documentos
Cultura Documentos
Elements: Statements, expressions,
variables and types
CS1P University of Glasgow John H. Williamson 2017 Python 3.x
Python
Python is an interpreted language. It does not compile code directly to machine
code.
This gives great flexibility in terms of what Python can do, but it means that many
checks that programs will behave sensibly cannot be done until code actually runs.
As we will see, Python is dynamically typed. This means it does not check that
variables or values have appropriate types for operations until those operations
actually happen. Python will happily try to add a number to a string if you do this:
2 + "two"
and only when Python gets to that point of the program can an error be raised.
Again, this offers enormous flexibility and means that code can be kept free of
superfluous type definitions. However, Python will not check that you are using the
right kind of values until code actually runs.
Errors
Python errors are essentially all runtime errors they can only occur when the
relevant part of code is running. The only exception are syntax errors which you
will see if you enter code that is not valid Python code (e.g. brackets mismatched,
wrong indentation, opened but not closed quotes).
The actual Python execution process is a little more complicated, but the details
aren't important for CS1P.
In [1]: this isn't python so it will generate a syntax error if I
run it
File "<ipythoninput1b2a7d6a0a3fc>", line 1
this isn't python so it will generate a syntax error if I run it
^
SyntaxError: invalid syntax
Comments
Note that in Python, comments begin with a hash symbol # and continute until the
end of a line. Python ignores everything after the # until the line ends.
In [2]: print("Welcome to CS1P") # this bit is ignored I can
write whatever I want
Welcome to CS1P
Uses of comments
There are some common uses of comments:
To document code.
Comments should explain what code does if it isn't obvious.
Don't write comments like:
# x increases by 1
x = x + 1
but do write commments like:
# find the extant user who is most likely to have
# this item
return sorted([u['p_has'] for u in users if u['exist
s'])
To explain potential problems
Code sometimes has hidden assumptions. To make it easier for someone
maintaing the code, document these assumptions (ideally these should be API
documentation, but sometimes this doesn't make sense)
# we have assumed there is at least one user here
# this will break otherwise!
return users[0]
To temporarily disable code
If you want to "turn off" some code while debugging or testing, you can "comment it
out":
# print "this message will never be printed :("
print "but this one will"
Always clean up commented out code later! Never leave commented code around
once you have finished testing. It pollutes the code base and makes it hard to read
code.
Expressions
Expressions are sequences of code that produce (or return) a value. For
example, these are all expressions:
In [3]: 5
2 + 3
"a sensible string"
94 / 8 * 31
Out[3]: 364.25
The value of the last expression is automatically printed by the notebook when you
run the cell.
Statements
Statements are sequences of code that do something. For example, which call
functions or assign values to variables. The following are all statements:
In [4]: x = 4
x = x + 1
if x==5: print("x is definitely 5")
while x>=0:
x = x 1
x is definitely 5
Assignment statements
A statement that binds a value to a name is called an assigment statement. This
creates or updates a variable.
An assignment has a left hand side (before the =) and a right hand side (after
the =).
speed_limit = 70
[lhs] = [rhs]
Any expression can be in rhs. The lhs must refer to a variable that can be bound.
In the simplest case, this will just be a name like speed_limit.
Expressions occur as parts of statements, such as on the right hand side of an
assignment:
statement
||
x = x + 5
|| expression
statement
||
print fn(32 * age)
|| expression
Programs as sequences of statements
A Python program is a sequence of statements. Each statement performs an
action. Expressions can form parts of statements; for example, the righthand side
of an assignment of a variable must be an expression. [The same is true of a
parameter to a function call, which we will see in more detail later.]
In [5]: # valid, 50 is an expression
age = 50
age = age + 1
# valid, age + 1 is an expression
age = age + 1
print(age)
# valid, "hungry" is a valid expression
mood_state = "hungry"
52
In [6]: # invalid, if expressions are statements
new_age = if age>50: 50
File "<ipythoninput67a79a3b7dc41>", line 2
new_age = if age>50: 50
^
SyntaxError: invalid syntax
Types in expressions
Like other programming languages you may have seen, Python has a bunch of
standard data types. This includes:
strings "hello" and 'there'. [Strings can be surrounded by double
quotes " or single quotes ' in Python whichever is more convenient.]
integers 64
floatingpoint numbers 0.5
booleans True and False (note the capitalisation in Python!)
as well various collection types like:
lists [1,2,3,4]
tuples (1,2,3,4) (like lists but cannot be modified)
dictionaries {age:50, mood_state:"hungry"}
One very key difference from languages such as C, Java and C# is that Python is
dynamically typed, not statically typed. The key consqeuence of this is that:
values have types, not variables
You may have seen languages where the type of a variable must be declared
before it is used, and it can then only hold values of that declared type:
int age;
age = 5;
String mood_state;
mood_state = "angry";
and statements like mood_state = 5; would be an error.
This is not the case in Python! Values have types, and a variable is simply a
binding of a name to a value.
Static typing
The static typing model has variables as "slots" of different shapes, which, once
created, only that type will fit into.
Dynamic typing
The dynamic typing model has values that know their own type, and live on their
own as complete objects somewhere in memory. Assigning them to a variable just
tells the interpreter to refer to that object with the given name.
The process of binding a name to a value is called assignment. Assignment is
notated with a single = sign in Python. Variables are not declared in advance of
use. Assigning instantly binds a name to a value.
In [7]: # assigns the string hungry to mood_state
mood_state = "hungry"
# this is fine
mood_state = "very angry"
# this is also fine; mood_state has no concept of
# what value it refers to
mood_state = 20
# this is also fine; mood_state can hold any value
mood_state = ["like", "totally", "fine"]
# now mood_state_copy refers to the same thing as mood_st
ate
mood_state_copy = mood_state
# reassigns mood_state; this binds mood_state to a new va
lue
mood_state = "calm"
## note that mood_state_copy is unchanged; it is still
## bound to the value that mood_state had when mood_state
_copy was last assigned
print(mood_state, mood_state_copy)
calm ['like', 'totally', 'fine']
Reflection
Python supports reflection, which just means investigating properties of values at
runtime. For example, we can see what type a value has using type().
In [8]: print(type(5))
print(type("hello"))
x = [1,2,3,4]
print(type(x))
<class 'int'>
<class 'str'>
<class 'list'>
We can also see attributes that values have using dir(). The output will be
pretty overwhelming at this stage, but the key point is that a type like int knows
how to do its own operations; a number like 5 carries around a reference to a bit
of code that will do addition when it is the operand of +, and a string like "5" carries
around a reference to a different bit of code that will do concatenation when it is an
operand of +.
In [9]: print(dir(5))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr_
_', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floo
rdiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '_
_hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '_
_le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new_
_', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduc
e__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__r
mul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub_
_', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__',
'__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugat
e', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
Syntactic sugar
a * b is a nice way to express the multiplication of numbers. "Under the hood"
Python actually translates this to a.__mul__(b); i.e. it calls a function called
__mul__ on the value a with the argument b.
In [10]: a = 10
b = 100
print(a + b)
print(a.__add__(b))
print(a * a)
print(a.__mul__(a))
110
110
100
100
Typing
How does Python know what to do when it gets a value? For example, compare:
In [11]: ## plus is used on numbers
age = 50 + 50
## plus is used on strings
name = "first" + "last"
print(age, name)
100 firstlast
In [12]: ## This is not allowed though:
## plus used on strings and numbers
"henry" * 8
Out[12]: 'henryhenryhenryhenryhenryhenryhenryhenry'
Duck typing
Python is dynamically types, as we have seen. More specifically, Python's typing
model is called duck typing because if a value walks like a duck and quacks like a
duck it is a duck.
[Image credit: Looli CCBYNC 2.0]
This means that Python avoids explicitly checking the type of values. Internally, it
doesn't, for example do things like:
def add(x,y):
if type(x)==int and type(y)==int:
return int_add(x,y)
if type(x)==float and type(y)==float:
return float_add(x,y)
if type(x)==string and type(y)==string:
return string_add(x,y)
Python trusts that you will send values that do what you, the programmer, say they
will.
In [13]:
Operators, operands, operations
Operators are elements of expressions that do some operation (for example, add
two numbers). They take operands, which are the values they operate upon.
Python operators are infix if they have two operands (appear between operands)
and prefix if they have one operand (appears before the operand).
One operand: unary operator: prefix.
Two operands: binary operator: infix.
Note that an operator like can perform different operations depending on where it
appears in between two operands, it is subtraction; before a number it is
negation.
Operator examples
8 + 9
operands: 8, 9
operator: + [binary]
operation: addition
"first" + "last"
operands: "first", "lasst"
operator: + [binary]
operation: concatenation (string joining)
8 9
operands: 8, 9
operator: [binary]
operation: subtraction
9
operands: 9
operator: [unary]
operation: negation
2 ** 8
operands: 2,8
operator: ** [binary]
operation: exponentiation
is_alive and is_kicking
operands: is_alive, is_kicking
operator: and [binary]
operation: logical/boolean and
word & 0xff
operands: bit_mask, 0x100
operator: & [binary]
Standard Python operators
These are all valid operators in Python:
lambda Lambda expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in Membership tests
is, is not Identity tests
<, <=, >, >=, !=, == Comparisons
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<, >> Bitwise Shifts
+, Addition and subtraction
*, /, % Multiplication, Division and
Remainder
+x, x Positive, Negative
~x Bitwise NOT
** Exponentiation
x.attribute Attribute reference
x[index] Indexing
x[index:index] Slicing
f(arguments ...) Function call
(expressions, ...) Binding or tuple display
[expressions, ...] List display
{key:datum, ...} Dictionary display
Many of these you will not have seen before, but the key arithmetic operators are
in the middle of the table.
Precedence
Python follows essentially the normal precedence rules from school algebra
(PEMDAS parenthesis, exponentiation, multiplication, division, addition,
subtraction).
But there are quite a number of extra operators provided with Python. All operators
have a precedence, which is the order the appear in the table above. Operators
with equal precedence (like +, ) are ordered from left to right.
An operator with higher precedence is said to bind tighter than one with lower
precedence, because it "binds" onto its operands more tightly. Multiplication, for
example, binds tighter than addition.
4 * 5 + 10
5 is "bound tighter" to 4 than to 10
25 * 10**2 + 1 7
( x + 1 ) * 2
Putting an expression in parenthesis (round brackets) forces Python to evaluate
the expression inside the brackets before applying any further operators. This can
be used to override the default precedence:
In [13]: print(1 + 2 * 3) # = 1 + 6 = 7
print((1+2) * 3) # = 3 * 3 = 9
print((((1+2)*3)+4)*5) # = 65; we can nest parentheses as
we deep as we wish
7
9
65
Using parentheses
If you're in any doubt about the order in which operators will act, then use
parentheses to be unambiguous. Everything within parenthesis is evaluated
before any other operation which applies to it.
x + 2 * y # compute 2*y, adds x
(x + 2) * y # computes x+2, multiplies by y
((((x*4)+5)*6)+8) # computes x*4, adds 5
# multiplies by 6, adds 8
In [14]: # I don't remember the order these will be applied in
# [don't worry, you don't need to know what these operato
rs do at this point!]
print(21 & 15 << 4 ^ 317)
# this is much clearer (and wasn't the default order!)
print(((21 & 15) << 4) ^ 317)
301
365
Overloaded operators
It's important to distinguish the symbols used for operators (like +, , *) from the
operations they apply. In Python, one operator symbol can have many meanings,
depending on what value (operand) it is applied to. For example:
2 + 3
>>> 6
"first" + "last"
>>> firstlast
In the first case, the + symbol meant integer addition. In the second case, + meant
string concatenation (joining). Python looks at the types of the operands and
decides what operation to apply when it sees an operator symbol. If there is no set
of matching types, an error occurs.
You can see this if you try to make a malformed expression:
In [15]: # this won't work you can't add strings and integers
5 + "string"
TypeError Traceback (most recent call last)
<ipythoninput15e5843beb52c7> in <module>()
1 # this won't work you can't add strings and integers
> 2 5 + "string"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
The operation done depends on the operator and the
type of the operands.
Some common operators
Equality
We can test values for equality using == (note the double equals!). In Python, ==
means "test if equal" and = means assign.
In [16]: print("same" == "same")
print("same" == "different")
True
False
Equality and assignment
Don't confuse these:
= means assign to variable. Read as "becomes".
x=5 : "x becomes 5"
== means compare value for equality. Read as "equals".
x==5 : "x equals 5?"
They look similar but are absolutely not interchangeable!
Not equal to
!= is the opposite of ==
In [17]: print("same" != "same")
print("same" != "different")
print(not("same" == "same"))
False
True
False
Ordering comparisons
The standard operators > (greater than) >= (greater than or equal to) < (less than)
<= (less than or equal to) allow you to compare the ordering of values.
In [18]: print(5>6)
print(6<5)
print(6>=6)
False
False
True
They also work for strings, in which case Python will compare the strings in
lexicographic (dictionary order).
In [19]: print("Aardvark" < "Zebra")
print("aaa" < "aab")
print("aaa" < "aaa") # aaa is *equal* to aaa, so cannot b
e < than
True
True
False
Comparisions only work between
comparable types
Be very careful to compare values of the same type. Python will usually not let you
do things like compare a number to a string, but there are exceptions.
In [20]: print("yes" > 6)
# what does this even mean?!
print("no" > False)
TypeError Traceback (most recent call last)
<ipythoninput204afaefcc6646> in <module>()
> 1 print("yes" > 6)
2
3 # what does this even mean?!
4 print("no" > False)
TypeError: '>' not supported between instances of 'str' and 'int'
In [21]: print(5>5.5) # comparing integers and floating point is f
ine the type is different, but they are comparable
False
Arithmetic operators
We have the standard arithmetic operators +, , *, /.
There are a couple of other useful ones as well:
**, which is exponentation. 2**8 is 256
'%', which is modulus (remainder). 15 % 4 is 3.
'//', which is integer division. 5/2 is 2.5, but 5//2 is 2
For strings, only + and * work (and % which we will see later). + joins two strings
and * repeats a string (not often useful!)
In [22]: 4 // 5
Out[22]: 0
In [23]: print(4/5)
0.8
In [24]: print("The song went: "+ "And the beat goes on... " *
10)
The song went: And the beat goes on... And the beat goes on... And the beat goes o
n... And the beat goes on... And the beat goes on... And the beat goes on... And th
e beat goes on... And the beat goes on... And the beat goes on... And the beat goes
on...
Boolean operators
Any expression that evaluates to a boolean result (either True or False), can be
manipulated using the and or and not operators.
not flips the operand following from True to False or vice versa
and is True if both operands are True
or is True if either operand is True.
In [25]: wind = 50
time = 22
stormy = wind > 20
night = time > 19
dark = night
light = not dark
In [26]: print((dark and night) or False)
print(dark and light)
print(dark or light)
print(stormy and (dark or night))
True
False
True
True
Assignment operators
As well as plain old = (meaning "assign right hand side to left hand side"), we have
convenience operators in Python to update the value of variables.
For example:
x_pos = x_pos + font_kerning
can be written as
x_pos += font_kerning
This reduces the amount of repetition in the code, and makes it clearer that we are
updating a variable, rather than giving it a totally new value.
In this case += means evaluate the right hand side and add it to x_pos. The
standard arithmetic operators all have assignment operators:
+=
=
*=
/=
%=
**= [I have never seen this one used!]
In [27]: b = 2
b += 2
b *= 3
print(b)
12
Identifiers
Identifiers are names that we can give to variables, functions and packages in
Python. There are specific rules that have to be followed:
Identifiers must start with a letter or _ (underscore)
They must consist only of letters, numbers or underscores
They must not be the same as any reserved keywords
There are a small number of reserved keywords in Python, like if, while,
for, return and a few others. Your variables cannot have these as names.
These rules ensure that you can't call a variable a*b and then be confused when
a*b does not multiply a and b together.
Python is case sensitive, and the case of identifiers matters! var does not refer to
the same variable as VAR or vAr.
In [28]: ## valid identifiers
x = 1
_name_ = 1
this_var = 1
x1 = 1
NAME = 1
_return = 1
gpio_32_18_1 = 1
___ = 1 # please don't use this!
In [29]: ## invalid identifiers
and = 5 # and is reserved
File "<ipythoninput29cea0341da158>", line 2
and = 5 # and is reserved
^
SyntaxError: invalid syntax
In [30]: return = 3 # return is reserved too
File "<ipythoninput30ddacba187bb2>", line 1
return = 3 # return is reserved too
^
SyntaxError: invalid syntax
Parallel assignment
Python has a nice facilty that allows you to assign multiple values in a single line of
code. Simply separate the values on both sides of the equals with commas:
In [31]: a, b = 1, 2
print(a, b)
1 2
Swapping variable values
One common use of this is to swap the values of two variables; because Python
will process a single assignment all at once, we can swap variables with
In [32]: a, b = "asphalt", "boots"
print(a,b)
## Note: this swaps a and b in one go
b, a = a, b
print(a,b)
## This is **not** the same and does **not** work!
b = a
a = b
print(a,b)
asphalt boots
boots asphalt
boots boots
Simple input and output
We've already seen this in examples, but we can print any value using print. If
we put multiple comma separated values, each value is printed out with a space
separating them.
Newlines
print puts a newline character at the end of each statement. If you want to omit
this, you can put specify that the end of the string should have no newline, using
end=''. This lets you split a single output line over several lines of code.
In [33]: print("hello")
print(5)
print("a", 2, "five")
## using end=''
print("hello",end='')
print("there",end='')
hello
5
a 2 five
hellothere
Input
Similarly, input() will read one line of text from the keyboard. It always returns a
string value. input() can take a string which it displays as a prompt.
In [34]: year = input("What year is this? ")
print("It's ", year, ", Richard")
StdinNotImplementedError Traceback (most recent call last)
<ipythoninput34a721b3cdbba0> in <module>()
> 1 year = input("What year is this? ")
2 print("It's ", year, ", Richard")
c:\conda3\lib\sitepackages\ipykernel\kernelbase.py in raw_input(self, prompt)
698 if not self._allow_stdin:
699 raise StdinNotImplementedError(
> 700 "raw_input was called, but this frontend does not support i
nput requests."
701 )
702 return self._input_request(str(prompt),
StdinNotImplementedError: raw_input was called, but this frontend does not support
input requests.
Type conversion
But what if we wanted to find the next year? We could add one to the value:
In [35]: ## This is an error!
year = input("What year is this? ")
print("It will soon be", year+1)
StdinNotImplementedError Traceback (most recent call last)
<ipythoninput35f4d6bae031b0> in <module>()
1 ## This is an error!
> 2 year = input("What year is this? ")
3 print("It will soon be", year+1)
c:\conda3\lib\sitepackages\ipykernel\kernelbase.py in raw_input(self, prompt)
698 if not self._allow_stdin:
699 raise StdinNotImplementedError(
> 700 "raw_input was called, but this frontend does not support i
nput requests."
701 )
702 return self._input_request(str(prompt),
StdinNotImplementedError: raw_input was called, but this frontend does not support
input requests.
But input() gave us a string and we can't add integers to strings. We can,
however, convert a string to an integer, or to a float, and vice versa.
Converting types
In Python, the functions that do this are:
int(x) convert x to an integer
float(x) convert x to a floatingpoint number
str(x) convert x to a string
[there are also functions like list(), tuple() etc. that we will see later]
Note that these functions do not alter the original value they create completely
new values and set them according to the value you gave them. They can take
strings, but can also take integers, floats or other number values.
Errors
It is an error to pass a string not interpretable as a number to the conversion
functions.
In [36]: print(int("2016"), type(int("2016")))
print(float("2016"), type(float("2016")))
print(str(2016), type(str(2016)))
2016 <class 'int'>
2016.0 <class 'float'>
2016 <class 'str'>
In [37]: # does your telephone number have a g in it?
print(int("01413306g657"))
ValueError Traceback (most recent call last)
<ipythoninput3740cb5e441900> in <module>()
1 # does your telephone number have a g in it?
> 2 print(int("01413306g657"))
ValueError: invalid literal for int() with base 10: '01413306g657'
In [38]: # int can take letters, if you are using a different base
(e.g. hexadecimal)
# 0xDEADBEEF in hex is 3735928559 in decimal
print(int("deadbeef", base=16))
3735928559
Formatted printing
We often want to insert values into strings; for example, to calculate a result and
place that into a message we print:
In [39]: active_users = 31
max_user_sessions = 12
print("Total maximum user sessions at the moment is %d se
ssions." % (active_users*max_user_sessions))
Total maximum user sessions at the moment is 372 sessions.
Here, we used the % operator on a string and a sequence of values (there was
only one in this case). The important thing is that the string has special character
sequences in it which tell the % operator how to insert the values on the right hand
side of the expression.
[string] % (value_1, value_2, value_3)
Each of these special sequences begins %, and the character after tells us what to
insert there:
%d means an integer
%f means a floating point number
%s means a string
%% means just put a percent here (otherwise the interpreter would
assume % was indicating a value to be inserted).
[there are others as well, but these are the key ones for now]
There must be exactly as many values on the right hand side as there are %
sequences in the string.
In [40]: # we can omit the brackets only if we have one value to i
nsert
print("The maximum speed was %f knots" % 20.5)
print("%s moved behind %s at %d o'clock" % ("Jupiter", "t
he Moon", 8))
print("%s: %dm %dm %dm" % ("echosounder_log", 60,
15, 12))
# note the use of the double % here
print("Engines operating at %d%%" % (80))
The maximum speed was 20.500000 knots
Jupiter moved behind the Moon at 8 o'clock
echosounder_log: 60m 15m 12m
Engines operating at 80%
In [41]: ### Error: too few formatting strings
print("My name is %s" % ("First", "Last"))
TypeError Traceback (most recent call last)
<ipythoninput410487026c1dc7> in <module>()
1 ### Error: too few formatting strings
> 2 print("My name is %s" % ("First", "Last"))
TypeError: not all arguments converted during string formatting
In [42]: ### Error: too many formatting strings
print("My name is %s %s %s" % ("First", "Last"))
TypeError Traceback (most recent call last)
<ipythoninput427b8c25dcd6c2> in <module>()
1 ### Error: too many formatting strings
> 2 print("My name is %s %s %s" % ("First", "Last"))
TypeError: not enough arguments for format string
In [43]: ### Error: told Python we'd give it an integer, gave it a
string instead
print("My name is %d" % ("First"))
TypeError Traceback (most recent call last)
<ipythoninput435958dd4f9483> in <module>()
1 ### Error: told Python we'd give it an integer, gave it a string instead
> 2 print("My name is %d" % ("First"))
TypeError: %d format: a number is required, not str
Question
What concerns you so far in CS1P (something missing, something unclear)? You
have 140 characters (1 Tweet length).
Syntax review [from
learnxinyminutes.com]
In [44]: ####################################################
## 1. Primitive Datatypes and Operators
####################################################
# You have numbers
3 # => 3
# Math is what you would expect
1 + 1 # => 2
8 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
# Result of integer division truncated down both for posi
tive and negative.
5 // 3 # => 1
5.0 // 3.0 # => 1.0 # works on floats too
5 // 3 # => 2
5.0 // 3.0 # => 2.0
# The result of division is always a float
10.0 / 3 # => 3.3333333333333335
# Modulo operation
7 % 3 # => 1
# Exponentiation (x**y, x to the yth power)
2**3 # => 8
# Enforce precedence with parentheses
(1 + 3) * 2 # => 8
# Boolean values are primitives (Note: the capitalizatio
n)
True
False
# negate with not
not True # => False
not False # => True
# Boolean Operators
# Note "and" and "or" are casesensitive
True and False # => False
False or True # => True
# Note using Bool operators with ints
# False is 0 and True is 1
# Don't mix up with bool(ints) and bitwise and/or (&,|)
0 and 2 # => 0
5 or 0 # => 5
0 == False # => True
2 == True # => False
1 == True # => True
5 != False != True #=> True
# Equality is ==
1 == 1 # => True
2 == 1 # => False
# Inequality is !=
1 != 1 # => False
2 != 1 # => True
# More comparisons
1 < 10 # => True
1 > 10 # => False
2 <= 2 # => True
2 >= 2 # => True
# Comparisons can be chained!
1 < 2 < 3 # => True
2 < 3 < 2 # => False
# Strings are created with " or '
"This is a string."
'This is also a string.'
# Strings can be added too! But try not to do this.
"Hello " + "world!" # => "Hello world!"
# String literals (but not variables) can be concatenated
without using '+'
"Hello " "world!" # => "Hello world!"
# A string can be treated like a list of characters
"This is a string"[0] # => 'T'
# You can find the length of a string
len("This is a string") # => 16
# If your Python 3 code also needs to run on Python 2.5 a
nd below, you can also
# still use the old style of formatting:
"%s can be %s the %s way" % ("Strings", "interpolated",
"old") # => "Strings can be interpolated the old way"
# Python has a print function
print("I'm Python. Nice to meet you!") # => I'm Python.
Nice to meet you!
# By default the print function also prints out a newline
at the end.
# Use the optional argument end to change the end string.
print("Hello, World", end="!") # => Hello, World!
# Simple way to get input data from console
input_string_var = input("Enter some data: ") # Returns t
he data as a string
# Note: In earlier versions of Python, input() method was
named as raw_input()
# There are no declarations, only assignments.
# Convention is to use lower_case_with_underscores
some_var = 5
some_var # => 5
# Accessing a previously unassigned variable is an except
ion.
# See Control Flow to learn more about exception handlin
g.
some_unknown_var # Raises a NameError
I'm Python. Nice to meet you!
Hello, World!
StdinNotImplementedError Traceback (most recent call last)
<ipythoninput448c706f2b237e> in <module>()
100
101 # Simple way to get input data from console
> 102 input_string_var = input("Enter some data: ") # Returns the data as a strin
g
103 # Note: In earlier versions of Python, input() method was named as raw_inpu
t()
104
c:\conda3\lib\sitepackages\ipykernel\kernelbase.py in raw_input(self, prompt)
698 if not self._allow_stdin:
699 raise StdinNotImplementedError(
> 700 "raw_input was called, but this frontend does not support i
nput requests."
701 )
702 return self._input_request(str(prompt),
StdinNotImplementedError: raw_input was called, but this frontend does not support
input requests.