In [5]:
x = 1
if x == 1:
    # indented four spaces
    print "x is 1."


x is 1.

Variable type

命名規則?

A valid identifier is a non-empty sequence of characters of any length with:

  • the start character can be the underscore "_" or a capital or lower case letter
  • the letters following the start character can be anything which is permitted as a start character plus the digits.
  • Just a warning for Windows-spoilt users: Identifiers are case-sensitive
  • Python keywords are not allowed as identifier names

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

LIST


In [3]:
mylist = [1,2,3,4,5,"test","test2"]

In [5]:
mylist


Out[5]:
[1, 2, 3, 4, 5, 'test', 'test2']

In [7]:
mylist[1]


Out[7]:
2

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]:
[4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

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


1
2
3
1
2
3

In [35]:
mylist = [1,2,3,4,5,6]
print(mylist[10])


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-35-af8112443d7b> in <module>()
      1 mylist = [1,2,3,4,5,6]
----> 2 print(mylist[10])

IndexError: list index out of range

In [50]:
mylist2


Out[50]:
[4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

In [67]:
mylist2[::-2]


Out[67]:
[22, 18, 14, 10, 6]

In [143]:
x = [1,2,3,4]

In [144]:
x[1:2]


Out[144]:
[2]

Dictionary


In [78]:
phonebook = {}
phonebook["John"] = {'age':42423,'tel':[938477566,412414124]}
phonebook["Jack"] = 938377264
phonebook["Jill"] = 947662781

In [80]:
phonebook  #key:value


Out[80]:
{'Jack': 938377264,
 'Jill': 947662781,
 'John': {'age': 42423, 'tel': [938477566, 412414124]}}

In [82]:
phonebook['John']['age']


Out[82]:
42423

In [3]:
phonebook = {
    "John" : 938477566,
    "Jack" : 938377264,
    "Jill" : 947662781
}

In [86]:
del phonebook["John"]

In [101]:
phonebook['Paul']=4924791827

In [102]:
phonebook


Out[102]:
{'Jack': 938377264, 'Jill': 947662781, 'Paul': 4924791827}

Operation


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]:
'hello hello hello hello hello hello hello hello hello hello '

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]:
[1, 3, 5, 7, 2, 4, 6, 8]

In [110]:
print [1,2,3] * 3


[1, 2, 3, 1, 2, 3, 1, 2, 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]:
5.5

In [126]:
x = 11
y = 2
print(float(x)/y)


5.5

In [132]:


In [133]:



Out[133]:
2384370293875403298570923875093L

String Formating


In [134]:
name = "John"
print "Hello, %s!" % name


Hello, John!

In [135]:
print "Hello " + name + "!"


Hello John!

In [136]:
# This prints out "John is 23 years old."
name = "John"
age = 23
print "%s is %d years old." % (name, age)


John is 23 years old.

In [137]:
# This prints out: A list: [1, 2, 3]
mylist = [1,2,3]
print "A list: %s" % mylist


A list: [1, 2, 3]
%s - String (or any object with a string representation, like numbers) %d - Integers %f - Floating point numbers %.f - Floating point numbers with a fixed amount of digits to the right of the dot. %x/%X - Integers in hex representation (lowercase/uppercase)

In [139]:
name


Out[139]:
'John'

In [140]:
print(name[0])
print(name[1])


J
o

In [142]:
name[1:3]


Out[142]:
'oh'

In [17]:
name[1:3]


Out[17]:
'oh'

In [145]:
name[::-1]


Out[145]:
'nhoJ'

In [29]:
name[0:4:2]


Out[29]:
'Jh'

In [30]:
name[0::2]


Out[30]:
'Jh'

In [31]:
name[::-1]


Out[31]:
'nhoJ'

In [158]:
x = 'I am a student'.split(' ')

In [160]:



Out[160]:
list

In [ ]: