LoginSignup
3
3

More than 5 years have passed since last update.

[WIP]Pythonの文法を学習する

Last updated at Posted at 2018-04-30

100個くらいサンプル書いたら覚えられるかな
Jupyterに書きながら学習中。
Windows+Anaconda3(Jupyter)にて学習環境構築。
(VS2017入れなおしたときにPythonサポートを追加した。インストーラー便利だな)

TODO:PEP8 LINTする

001 ハローワールド

q001.py
print("hello world")

002 コメントの書き方

q002.py
#COMMENT GOES HERE

003 関数の後ろにコメント

q003.py
print("hello world") #comment

004 ブロックコメント

q004.py
'''
print("hello world1")
print("hello world2")
'''

005 文字列の行継続

q005.py
print('''hello world1
hello world2
hello world3''')

006 printの終端を改行ではなく任意の文字にする

q006.py
print("たこ",end="★")
print("いか")

007 カンマで区切ると 空白区切りになる

q007.py
print("いぬ","さる","きじ")

008 文字列+文字列=文字列連結

q008.py
hero = "メロス"
print(hero +"は走った")

009 数値と文字列を連結する場合はstr()

q009.py
n=10
#print(n+"億円ほしい!!") #実行時エラー
print(str(n)+"億円ほしい!!")

010 乱数生成

q010.py
import random
random.random()
n=random.randint(1, 100)
print("テストの点数は" + str(n) + "です。")

011 int+strはだめ

q011.py
p=100+"円"
----
TypeError: unsupported operand type(s) for +: 'int' and 'str'

012 if文による条件分岐

  • if/elseの行末のコロンが必要
  • 処理はインデントさせて書く
  • equal演算子は == とイコールを2回重ねて書く
q012.py
number = 1
if number == 1:
    print( "one")   #条件式が成立したときの処理
else:
    print( "not one")   #条件式が成立しなかったときの処理

013 else + if =elif

q013.py

if 条件式1:
    print( "c1")    #条件式1が成立したときの処理
elif 条件式2:
    print( "!c1 & c2")  #(条件式1が成立していない状態で)条件式2が成立したときの処理
else:
    print( "!c1 & !c2") #条件式がどれも成立しなかったときの処理

014 比較演算子

pep8_q014.py
if number == 5:
    print("5と等しい")

if number > 5:
    print("5より大きい")

if number < 5:
    print("5より小さい")

if number >= 5:
    print("5以上")

if number <= 5:
    print("5以下")

if number != 5:
    print("5と等しくない")
3
3
9

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
3
3