In [1]:
import keyword
print(keyword.kwlist)
In [2]:
# 这是一个单行注释
'''
这是一个多行注释
'''
print('Hello world')
In [3]:
for i in range(3):
print(i)
In [4]:
for i in range(3):
print(i)
In [5]:
item_one = 1
item_two = 2
item_three = 3
total = item_one + \
item_two + \
item_three
total
Out[5]:
In [6]:
a = 1
b = 'apple'
print('a = %d and b = %s' % (a, b))
In [7]:
print('a = {} and b = {}'.format(a, b))
In [8]:
print('a = {1} and b = {0}'.format(a, b))
In [9]:
a = 1
b = 0.2
c = 'Hello'
print('type(a) = {}, a = {}'.format(type(a), a))
print('type(b) = {}, b = {}'.format(type(b), b))
print('type(c) = {}, c = {}'.format(type(c), c))
In [10]:
tupleA = (1, 2, 3, 4)
tupleA = (1, 2, 3, 'apple')
print(tupleA[0])
print(tupleA[0:2])
In [11]:
tupleA[0] = 1
In [12]:
listA = [1, 2, 3, 4]
print('listA中第一个元素为:%s' % (listA[0]))
print('listA中前两个元素为:%s' % (listA[0:2]))
listA[0] = 'apple'
listA
Out[12]:
In [13]:
a = {1, 2}
b = {2, 3}
print('a和b的并集为:{}'.format(a | b))
print('a和b的交集为:{}'.format(a & b))
print('a和b的差集为:{}'.format(a - b))
In [14]:
data = {'name':'马旭辉', 'gender':'男'}
print(data['name'])
In [15]:
import time
import numpy as np
print(time.asctime())
print(np.array([1, 2, 3, 4]))
In [16]:
a = 5
if a>3:
print('a > 3')
elif a==3:
print('a == 3')
else:
print('a < 3')
In [17]:
a = 100
if a:
print('a != 0')
else:
print('a == 0')
In [18]:
count = 0
while count<10:
count += 1
print(count)
In [19]:
a = ['R', 'Python', 'Java', 'Ruby', 'Javascript']
# 一个非常不好的用法
# for i in range(len(a)):
# print(a[i])
# 应该这样写
for item in a:
print(item)
# 或许你需要用到索引
for i,item in enumerate(a):
print('%d : %s' % (i, item))
In [20]:
for i in range(5):
print(i)
In [21]:
def myadd(a, b):
result = a + b
return result
print(myadd(1, 2))