投稿

Built-In String Methods/Functions

>>> "aaa".capitalize() 'Aaa' >>> "Hi, Mac, Hello".casefold() 'hi, mac, hello' >>> "123".center(6) ' 123 ' >>> print("123".center(6)) 123 >>> print("123".center(10,"z")) zzz123zzzz >>> str="I love Python. Python is a programming language" >>> str.count("Python") 2 >>> text="Python is easy to learn." >>> text.endswith('to learn.') True >>> str='xyz\t1234\tzzz\t45' >>> str.expandtabs() 'xyz 1234 zzz 45' >>> str="Let me test this string" >>> str.find("test") 7 >>> str.find("that") -1 >>> print("Hello {}, my fav no is {}.".format("Tom", 888)) Hello Tom, my fav no is 888.

turtle circle drawing

イメージ
from turtle import * screensize(canvwidth=200,canvheight=200) speed("fastest") hideturtle() for i in range(10): for j in range(10): if i % 2 ==0 and j%2 ==0: fillcolor("green") elif i % 2 == 0 and j%2 ==1: fillcolor("yellow") elif i % 2 == 1 and j%2 ==0: fillcolor("pink") elif i % 2 == 1 and j%2 ==1: fillcolor("blue") up() setx(i*20) sety(j*20) down() begin_fill() circle(10) end_fill()

turtle drawing 2

イメージ
from turtle import * screensize(canvwidth=100,canvheight=100) speed("fastest") up() sety(250) setx(100) down() hideturtle() def drawLine(): for i in range(9): forward(12*i) right(30) for i in range(0,20): drawLine() right(36)

turtle drawing

イメージ
from turtle import * screensize(canvwidth=100,canvheight=100) speed("fastest") up() sety(150) down() hideturtle() def drawLine(): for i in range(7): forward(15*i) right(30) for i in range(0,60): drawLine() right(36)

What is list and set in Python?

Lists and tuples are standard Python data types that store values in a sequence. Sets are another standard Python data type that also store values. The major difference is that sets, unlike lists or tuples, cannot have multiple occurrences of the same element and store unordered values. What does set () do in Python? set() method is used to convert any of the iterable to sequence of iterable elements with distinct elements, commonly called Set. Parameters : Any iterable sequence like list, tuple or dictionary. Returns : An empty set if no element is passed. Non-repeating element iterable modified as passed as argument.

任意個の体重を入力し平均を計算する

イメージ
# calculate average weight wlist=[] for i in range(0,500): mes="Input your weight (0 exit): "+str(i)+" " w=input(mes) iw=int(w) if iw==0: break wlist.append(iw) print(wlist) print("input number = %d " % len(wlist)) print("sum = %d average = %d " %(sum(wlist) , sum(wlist)/len(wlist))) 補足:体重の値に0が入力されるまで、500人以下なら何人でも体重の値を受け付け、 その平均値を求めます。

Basic List Operations

>>> >>> len([1,2,3]) 3 >>> [1,2,3]+[4,5,6] [1, 2, 3, 4, 5, 6] >>> ["hello! "]*3 ['hello! ', 'hello! ', 'hello! '] >>> 3 in [1,2,3] True >>> max([1,2,3,4]) 4 >>> min([1,2,3]) 1 >>> list((1,2,3)) #converts a tuple into list [1, 2, 3] >>> for x in [10,20,30]: print(x) 10 20 30 >>>