スピーチマーク

TED・YouTube・書籍から学んだこと + Diary in English

Python - Hello World!

今日は英語日記にて失礼します。

 

Recently, I started learning Python.

The tutorial book that I bought begins lecturing “Hello World!”.

Here is what I wrote:

# Practise 01 - First time use of print command
print('Practise 01 - Hello World!')

# Practise 02 - Use of comma
# カンマで区切ると、スペースが追加される。
# カンマを使わなければ、区切られない。
print('Practise 02 - ''Hello','World!')

# Practise 03 - 文字列の連結を+(プラスサイン)で
a = 'Practise 03 - '
b = 'Hello '
c = 'World!'
d = a+b+c
print(d)

# Practise 04 - 文字列の繰り返し
a = 'Hello! '
b = a*3
print('Practise 04 -',b) 

# Practise 05 - 文字列中の文字
a = 'Hello'
b = 2
# bはインデックス番号と呼ぶ。
# 文字列aのインデックス番号b(b+1文字目)を、.formatを使って表示する
print('Practise 05 - {}の{}文字目は{}です。'.format(a,b+1,a[b]))

# Practise 06 - 文字列中の文字 (その2)
# f{value}で表示する。
print(f'Practise 06 - {a}の{b+1}文字目は{a[b]}です。')

# Practise 07 - 文字列中の文字 (その3)
# 多分、この書き方はしなさそう・・・
b = 1
print('Practise 07 - {1}は{0}の中の{2}文字目にあります。'.format(a,a[b],b+1))

# Practise 08 - ダブルクォーテーション
# 文字列の挟み方はシングルクォーテーションか,もしくはダブルクォーテーションでもいい。
print("Practise 08 - Hello World!")

# Practise 09 - エスケープシーケンス
# Use \n (new line) and \t (tab)
print('\nPractise 09 - Escape sequence\n\tHello\n')

# Practise 10 - ''' to continue the text in next line 
print('''Practise 10 - Show texts in multiple lines using \'''
\tHello
''')

# Practise 11 - 書式
# {:書式}の練習
a = 123456789
print(f'Practise 11 - In JPY, this product costs \\{a:,}.')
print(f'\t{a:>15}')   #空白を含めて15文字で表示(右揃え)
print(f'\t{a:<15}')   #空白を含めて15文字で表示(左揃え)
print(f'\t{a:^15}\n') #空白を含めて15文字で表示(中央揃え)

# Practise 12 - 書式の練習 その2
# %d %f
print('Practise 12 - 書式の練習(その2)')
print('\t%4d' %25)    #空白を含めて4文字で表示(右揃え)
print('\t%-4d' %25)   #空白を含めて4文字で表示(左揃え)
print('\t%04d' %25)   #0を使って4文字で表示
print('\t%.3f\n' %0.1)  #小数点以下を3文字で表示

# Practise 13 - Read input from keyboard
name = input('Practise 13 - Enter your name: ')
print(f'\tHello, {name}!\n')

# Practise 14 - 区切り文字、行末の指定
print('H','e','l','l','o',sep='ー', end='ー!')

 

Executing the above script gives the following output:

Practise 01 - Hello World!
Practise 02 - Hello World!
Practise 03 - Hello World!
Practise 04 - Hello! Hello! Hello!
Practise 05 - Helloの3文字目はlです。
Practise 06 - Helloの3文字目はlです。
Practise 07 - eはHelloの中の2文字目にあります。
Practise 08 - Hello World!

Practise 09 - Escape sequence
        Hello

Practise 10 - Show texts in multiple lines using '''
        Hello

Practise 11 - In JPY, this product costs \123,456,789.
              123456789
        123456789
           123456789

Practise 12 - 書式の練習(その2)
          25
        25
        0025
        0.100

Practise 13 - Enter your name: YASU
        Hello, YASU!

Hーeーlーlーoー!

 

It’s just a beginning.

I’m way too far from the use of Python libraries yet.

But at least I am happy that I have started learning Python

 

Happy Learning!

 

 

If you like this article, please vote by clicking one of the three icons shown below:

にほんブログ村 英語ブログへ にほんブログ村 英語ブログ 英語学習者へ にほんブログ村 英語ブログ 英語情報へ

Please also feel free to leave a comment in this blog.

Thank you! 😊