Basic grammer
if 5 > 2:
print("Five is greater than two!")
# Five is greater than two!
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
# This is a comment.
"""
This is a comment
written in
more than just one line
"""
- 문자 또는 _로 시작
- 숫자로 시작할 수 없음
- 알파벳, 숫자, _만 사용
- 대소문자 구분
Creating Variables
x = 5 # int
y = "Apple" # str
a, b, c = "Apple", "Banana", "Melon"
d = e = f = "Orange"
print(x)
print(y)
Casting, specify the data type of a variable
x = str(3) # '3'
y = int(3) # 3
z = float(3) # 3.0
Single or Double Quotes
x = "Apple"
x = 'Apple' # same value
변수이름 작성법
- Camel Case: myVarName = "Apple"
- Pascal Case: MyVarName = "Apple"
- Snake Case: my_var_name = "Apple"
Unpack Collection
fruits = ["apple", "banana", "cherry"] # list
x, y, z = fruits # Unpack
# x = "apple", y = "banana", z = "cherry"
Output Variables: print()
x = "hello"
y = " world"
print(x) # hello
print(x, y) # hello world
print(x + y) # hello world
x = 5
y = 10
z = "Apple"
print(x + y) # 15
print(x + z) # error
print(x, z) # 5 Apple
Global & Local Variables
x = "Apple" # global variable
def myfunc():
x = "Orange" # local variable
print(x + " is delicious")
print(x) # Apple: Global variable
myfunc() # Orange is delicious: Local variable
Global Keyword
def myfunc():
global x
x = "Apple"
print(x + " is delicious")
myfunc() # Apple is delicious
print(x) # Apple
Create a Module: .py
# mymodule.py
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Use a Module
import mymodule
mymodule.greeting("Jonathan")
Variables in Module
import mymodule
a = mymodule.person1["age"]
print(a)
alias: as
import mymodule as mx
a = mx.person1["age"]
print(a)
Built-in Modules
import platform
x = platform.system()
print(x)
dir()
- list all defined names in module
x = dir(platform)
print(x)
Import From Module
from mymodule import person1
print (person1["age"])
# from으로 부른 모듈의 부분을 사용할 때는 모듈이름을 붙이지 않음
User Input
username = input("Enter username:")