CCC: coding crash course (2)

Internal material.

Today's content:

-(1) Get used to list, append ()

-(2) Functions int (), float ()

-(3) Functions range (), list (range ())

-(4) for statement

-(5) Review questions


(1) Get used to list, append ()

--ʻAppend (A)` adds element A to the end of the list.

#review
#What is output?
a = [7, 8, 'taro', 'jack']

print(a)

print(a[2])

#What is output?
a = [7, 8, 'taro', 'jack']

a.append('10')

print(a)

a.append(10)

print(a)

#10 and'10'Is another.

# [7, 8, 'taro', 'jack', '10', 10]

--Tips for specifying the position of the list: [-1] = The minus 1st position is the end.

#What is output?
a = [7, 8, 'taro', 'jack']
print(a)

print(a[3])
print(a[-1])

#When the number of elements is 4, the address"[3]"And address"[-1]"Is the same.

--You can also put a list in the list.


#What is output?
a = [7, 8, 'taro', 'jack']
b = ['ML', 3.14]
a.append(b)
print(b)

print(a[-1])
print(a[-1][0])
print(a[-1][1])


(2) Functions int (), float ()

--ʻInt (A) : Convert A to an integer. --float (A)`: Convert A to a real number.



#What is output?

b = '5'
c = int(b)

print(b)
print(c)


d = 5.5
e = int(d)

print(d)
print(e)


f = 1.234
g = float(f)

print(f)
print(g)

(3) range(), list(range())

--ʻInt (A) : Convert A to an integer. --float (A): Convert A to a real number. --list (A)`: Convert A to list type.


A = range(10)
print(A)

B = list(range(10))
print(B)

C = list(range(1,11))
print(C)

D = list(range(1,11,2))
print(D)

E = list(range(11,1,-2))
print(E)

# [2000,2004,2008,2012,2016,2020]How can I display?

# [2020,2016,2012,2008,2004,2000]How can I display?

The output result of print (range (5)) is different between Python2 and Python3.


# python3
$ print(range(5))
range(0,5)
# python2
$ print(range(5))
[0,1,2,3,4]

(4) for statement [for loop]

--If the content to be repeated can be summarized in one line

for [rules for repetition]: \ _ Tab \ _ [Repeat]

――If the content to be repeated spans two lines

for [rules for repetition]: \ _ Tab \ _ [Repeat] \ _ Tab \ _ [Repeat]

Make blocks using tabs like this. This is a Python rule designed to make it easier to read (more readable). Failure to do this in a for loop will result in errors and unintended consequences.

For example list_a = ['Tokyo', 'Cambridge', 'Ann Arbor', 'Pittsburgh'] When displaying the list in order, the following two are often used.

--Turn "serial number ʻi" to 0-3 to display "ʻith element in the list"

for i in range(4): \ _ Tab \ _ print (list_a [i])

--"Take out the elements of the list one by one and display them"

for a in list_a: \ _ Tab \ _ print (a)


list_name = ['Taro', 'Jack', 'Tom', 'Andy', 'Jason']

print(list_name)
for x in list_name:
    print(x)

print('---')

for i in range(10):
    print(i)

print('---')
for i in range(2000,2021,4):
    print(i)

print('---')
for i in range(2020,1999,-4):
    print(i)

print('---')

N_people = len(list_name)

for i in range(N_people):
    print(i, list_name[i])


for i in range(0,N_people,2):
    print(i, list_name[i])
    print('--. . . --')

(5) Review questions

# Q.What do you think you will see?
list_name = ['Taro', 'Jack', 'Tom', 'Andy', 'Jason', 'Matt']
list_name.append('John')
list_name.append('Madoka')
list_name.append('Ichiro')
print(list_name)

N_people = len(list_name)
print(N_people)

for i in range(1,N_people,3):
    print(i, list_name[i])



(6) if statement

ʻIf (condition A): \ _ Tab \ _Read if" condition A is met "`

ʻIf (condition A): \ _ Tab \ _Read if" condition A is met " ʻElse:# Other \ _ Tab \ _ Read if" condition A is not met "

ʻIf (condition A): \ _ Tab \ _" Satisfy condition A " ʻElif (Condition B): \ _ Tab \ _ " Condition A is not met "and" Condition B is met " ʻElse: # Other \ _ Tab \ _ " Condition A is not met "and" Condition B is not met "`


x = 5
if (x>0):
    print('x=', x, '(This is larger than 0.)')

x = -3
if (x>0):
    print('x=', x, '(This is larger than 0.)')
else :
    print('x=', x, '(This is NOT larger than 0.)')


x = -3
if (x>0):
    print('x=', x, '(x>0)')
elif (x>-10) :
    print('x=', x, '(-10 < x < 0)')
else :
    print('x=', x, '(otherwise)')
    

Recommended Posts

CCC: coding crash course (1)
CCC: coding crash course (2)
CCC: coding crash course (4) Make the numbers appearing in pi 3.141562 .... into a histogram
CCC: coding crash course (5) Find out the frequency of words and letters that appear in Steve Jobs speeches