0
0

More than 1 year has passed since last update.

Pythonチュートリアル(第4版)3章 気楽な入門編

Posted at

はじめに

Pythonエンジニア認定試験に興味をもったので、
Pythonチュートリアル(第4版)をひたすら写経していきたくなったので、
写経したいと思います。(コード部分のみ)
ただし、プロンプト表記のところは全部pyファイル化して書きなおします。
今回は3章部分のみです。

3章 気楽な入門編

3.1 Pythonを電卓として使う

3.1.1 数値

a = 2 + 2
b = 50 - 5*6
c = (50 - 5*6) / 4
d =  8 / 5

# 4 20 5.0 1.6
# <class 'int'> <class 'int'> <class 'float'> <class 'float'>
print(a, b, c, d)
print(type(a), type(b), type(c), type(d))

e = 17 / 3
f = 17 // 3
g = 17 % 3
h = 5 * 3 + 2

# 5.666666666666667 5 2 17
# <class 'float'> <class 'int'> <class 'int'> <class 'int'>
print(e, f, g, h)
print(type(e), type(f), type(g), type(h))

i = 5 ** 2
j = 2 ** 7

# 25 128
# <class 'int'> <class 'int'>
print(i, j)
print(type(i), type(j))

width = 20
height = 5 * 9

# 900
print(width * height)

k = 4 * 3.75 - 1
# 14.0
# <class 'float'>
print(k)
print(type(k))

tax = 12.5 / 100
price = 100.50
l = round(price + (price * tax), 2)

# 113.06
# <class 'float'>
print(l)
print(type(l))

3.1.2 文字列

a = 'spam eggs'
b = 'doesn\'t'
c = "doesn't"
d = '"Yes," they said.'
e = "\"Yes,\" they said."
f = '"Isn\'t," they said.'

# spam eggs doesn't doesn't "Yes," they said. "Yes," they said. "Isn't," they said.
print(a ,b ,c ,d ,e ,f)

# "Isn't," they said.
print('"Isn\'t," they said.')
s = 'First line.\nSecond line.'

# First line.
# Second line.
print(s)

# C:\some
# ame
print('C:\some\name')
# C:\some\name
print(r'C:\some\name')

# Usage: thingy [OPTIONS]
#      -h                         Display this usage message
#      -H hostname                Hostname to connect to
print("""\
Usage: thingy [OPTIONS]
     -h                         Display this usage message
     -H hostname                Hostname to connect to
""")

# unununium
print(3 * 'un' + 'ium')

# Python
print('Py' 'thon')

text = ('カッコの中にながいながいながい文字列を'
        '入れておいて繋げてやろう。')

# カッコの中にながいながいながい文字列を入れておいて繋げてやろう。
print(text)

# 変数とリテラルの連結、そして変数同士の連結には+を使う
prefix = 'Py'
# Python
print(prefix + 'thon')

word = 'Python'
# P
print(word[0])
# n
print(word[5])
# n
print(word[-1])
# p
print(word[-2])
# P
print(word[-6])

# Py
print(word[0:2])
# tho
print(word[2:5])

# Python
print(word[:2] + word[2:])
# Python
print(word[:4] + word[4:])

# Py
print(word[:2])
# on
print(word[4:])
# on
print(word[-2:])

# on
print(word[4:42])
# 
print(word[42:])

# word[0] = J # エラー
# word[2:] = 'py' # エラー

word2 = 'J' + word[1:]
word3 = word[:2] + 'py'
# Jython Pypy
print(word2, word3)

s = 'supercalifragilisticexpialidocioud'
# 34
print(len(s))

3.1.3 リスト

squares = [1, 4, 9, 16, 25]
# [1, 4, 9, 16, 25]
print(squares)
# 1
print(squares[0])
# 25
print(squares[-1])
# [9, 16, 25]
print(squares[-3:])
# [1, 4, 9, 16, 25]
print(squares[:])

squares2 = squares + [36, 49, 64, 81, 100]
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squares2)

cubes = [1, 8, 27, 65, 125]
cubes[3] = 4 ** 3 # 64
# [1, 8, 27, 64, 125]
print(cubes)

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(letters)

letters[2:5] = ['C', 'D', 'E']
# ['a', 'b', 'C', 'D', 'E', 'f', 'g']
print(letters)

letters[2:5] = []
# ['a', 'b', 'f', 'g']
print(letters)

letters[:] = []
# []
print(letters)

letters = ['a', 'b', 'c', 'd']
# 4
print(len(letters))

a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
# [['a', 'b', 'c'], [1, 2, 3]]
print(x)
# ['a', 'b', 'c']
print(x[0])
# b
print(x[0][1])

3.2 プログラミング、はじめの一歩

# フィボナッチ級数
# 2項の和により次項が定まる
a, b = 0, 1

# 0
# 1
# 1
# 2
# 3
# 5
# 8
while a < 10:
    print(a)
    a, b = b, a+b

i = 256*256
# The value of i i is 65536
print('The value of i i is', i)

a, b = 0, 1

# 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
while a < 1000:
    print(a, end=',')
    a, b = b, a+b
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0