三歩あるけば物も忘れる

お腹のお肉がメタボックル

ユーザ用ツール

サイト用ツール


dokuwiki:draft:Practice

見出しレベル1

ここに見出しレベル1のテキスト

見出しレベル2

ここに見出しレベル2のテキスト(以下同様)

見出しレベル3

###
num = 1
name = 'mnz'

print(num, type(num), name, type(name))
print('Hi', 'Mike', sep=',', end='\n')
print('Hi', 'Mike', sep=',', end='.\n')

###
print(2 + 2)
print(17 / 3)
print(17 // 3)
print(17 % 3)
print(5 ** 5)
print(round(3.141516, 2))


###
import math
math.sqrt(25)
math.log2(10)

print(help(math))

###
print('I don\'t know')
print('say "I don\'t know"')
print("say \"I don't know\"")
print(r'c:\mnz\mnz')

print("""
line1
line2
line3
""")

print('#########')
print("""\
line1
line2
line3\
""")
print('#########')

print('Py''thon')
print('Py' + 'thon')

prefix = 'Py'
print(prefix'thon') ###NG
print(prefix + 'thon')

s = ('aaaaaaa'
     'bbbbbb')
print(s)

s = 'aaaaaaa'\
    'bbbbbb'
print(s)


word = 'Python'
print(word[0])
print(word[1])
print(word[-1])
print(word[0:2])
print(word[:2])
print(word[2:])

word = 'J' + word[1:]
print(word)
print(len(word))

### リスト
l = [1, 2, 3, 4, 5, 6]
print (l[0])
print (l[::2])

### ネスト
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
x
[['a', 'b', 'c'], [1, 2, 3]]
x [0][1]
'b'

### リスト操作
s = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
s
['a', 'b', 'c', 'd', 'e', 'f', 'g']

s[0] = 'x'
s
['x', 'b', 'c', 'd', 'e', 'f', 'g']

s[2:5] = ['C', 'D', 'E']
s
['x', 'b', 'C', 'D', 'E', 'f', 'g']
s[2:5] = []
s
['a', 'b', 'f', 'g']

s[:] = []
s
[]

n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n.append(100)
n
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
n.insert(0, 200)
n
[200, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
n.pop()
100
n
[200, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n.pop(0)
200
n
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del n[0]
n
[2, 3, 4, 5, 6, 7, 8, 9, 10]
del n
n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
n = [1, 2, 2, 2, 3]
n.remove(2)
n
[1, 2, 2, 3]
n.remove(2)
n.remove(2)
n
[1, 3]
n.remove(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
a += b
a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = [1, 2, 3, 4, 5]
x.extend(b)
x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

### リストのメソッド
r = [1, 2, 3, 4, 1, 2, 3]
r.index(3)
2
r.index(3, 3)
6
r.count(3)
if 4 in r:
...  print(r)
...
[1, 2, 3, 4, 1, 2, 3]
if 4 in r:
...  print('exist')
...
exist

r.sort()
r
[1, 1, 2, 2, 3, 3, 4]

r.sort(reverse=True)
r
[4, 3, 3, 2, 2, 1, 1]

r.sort()
r
[1, 1, 2, 2, 3, 3, 4]
r.sort(reverse=True)
r
[4, 3, 3, 2, 2, 1, 1]
r.re
r.remove(  r.reverse(
r.reverse()
r
[1, 1, 2, 2, 3, 3, 4]
s = 'My name is Mike.'
to_split = s.split(' ')
to_split
['My', 'name', 'is', 'Mike.']

x = ' '.join(to_split)
x
'My name is Mike.'


i = [1, 2, 3, 4, 5]
j = i
j[0] = 100
i, j
([100, 2, 3, 4, 5], [100, 2, 3, 4, 5])

j = i.copy()
j[0] = 100
i, j
([1, 2, 3, 4, 5], [100, 2, 3, 4, 5])

###################
seat = []
min = 0
max = 5
min <= len(seat) < max
True
seat.append('p')
seat
['p']
seat.append('p')
seat.append('p')
seat.append('p')
seat.append('p')
min <= len(seat) < max
False

コメント

コメントを入力:
X K A G X
 
dokuwiki/draft/Practice.txt · 最終更新: 2023/01/07 by admin