In [5]:
x = 1
if x == 1:
# indented four spaces
print "x is 1."
A valid identifier is a non-empty sequence of characters of any length with:
In [ ]:
In [2]:
myfloat = 7.0
myfloat = float(7)
In [3]:
mystring = 'hello'
mystring = "hello"
In [4]:
one = 1
two = 2
three = one + two
hello = "hello"
world = "world"
helloworld = hello + " " + world
In [8]:
a, b = 3, 4
In [3]:
mylist = [1,2,3,4,5,"test","test2"]
In [5]:
mylist
Out[5]:
In [7]:
mylist[1]
Out[7]:
In [10]:
mylist = []
for i in range(100):
mylist.append(i)
In [48]:
mylist2 = [(i+2)*2 for i in range(10)]
In [49]:
mylist2
Out[49]:
In [9]:
mylist = []
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3
# prints out 1,2,3
for x in mylist:
print x
In [35]:
mylist = [1,2,3,4,5,6]
print(mylist[10])
In [50]:
mylist2
Out[50]:
In [67]:
mylist2[::-2]
Out[67]:
In [143]:
x = [1,2,3,4]
In [144]:
x[1:2]
Out[144]:
In [78]:
phonebook = {}
phonebook["John"] = {'age':42423,'tel':[938477566,412414124]}
phonebook["Jack"] = 938377264
phonebook["Jill"] = 947662781
In [80]:
phonebook #key:value
Out[80]:
In [82]:
phonebook['John']['age']
Out[82]:
In [3]:
phonebook = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
In [86]:
del phonebook["John"]
In [101]:
phonebook['Paul']=4924791827
In [102]:
phonebook
Out[102]:
In [12]:
number = 1 + 2 * 3 / 4.0
In [13]:
remainder = 11 % 3
In [14]:
squared = 7 ** 2
cubed = 2 ** 3
In [105]:
lotsofhellos = "hello " * 10
In [106]:
lotsofhellos
Out[106]:
In [108]:
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
In [109]:
all_numbers
Out[109]:
In [110]:
print [1,2,3] * 3
Operator | Description | Example |
---|---|---|
+, - | Addition, Subtraction | 10 -3 |
*, /, % | Multiplication, Division, Modulo | 27 % 7 Result: 6 |
// | Truncation Division (also known as floordivision) The result of the division is truncated to an integer. Works for integers and floating-point numbers as well | 27 // 7 Result: 6 |
+x, -x | Unary minus and Unary plus (Algebraic signs) | -3 |
~x | Bitwise negation | ~3 - 4 Result: -8 |
** | Exponentiation | 10 ** 3 Result: 1000 |
or, and, not | Boolean Or, Boolean And, Boolean Not | (a or b) and c |
in | "Element of" | 1 in [3, 2, 1] |
<, <=, >, >=, !=, == | The usual comparison operators | 2 <= 3 |
, &, ^ | Bitwise Or, Bitwise And, Bitwise XOR | 6 ^ 3 |
<<, >> | Shift Operators | 6 << 3 |
In [122]:
11.0/2
Out[122]:
In [126]:
x = 11
y = 2
print(float(x)/y)
In [132]:
In [133]:
Out[133]:
In [134]:
name = "John"
print "Hello, %s!" % name
In [135]:
print "Hello " + name + "!"
In [136]:
# This prints out "John is 23 years old."
name = "John"
age = 23
print "%s is %d years old." % (name, age)
In [137]:
# This prints out: A list: [1, 2, 3]
mylist = [1,2,3]
print "A list: %s" % mylist
In [139]:
name
Out[139]:
In [140]:
print(name[0])
print(name[1])
In [142]:
name[1:3]
Out[142]:
In [17]:
name[1:3]
Out[17]:
In [145]:
name[::-1]
Out[145]:
In [29]:
name[0:4:2]
Out[29]:
In [30]:
name[0::2]
Out[30]:
In [31]:
name[::-1]
Out[31]:
In [158]:
x = 'I am a student'.split(' ')
In [160]:
Out[160]:
In [ ]: