Basic grammer
if 5 > 2:
print("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
written in
more than just one line
"""
- 문자 또는 _로 시작
- 숫자로 시작할 수 없음
- 알파벳, 숫자, _만 사용
- 대소문자 구분
Creating Variables
x = 5
y = "Apple"
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)
y = int(3)
z = float(3)
Single or Double Quotes
x = "Apple"
x = 'Apple'
변수이름 작성법
- Camel Case: myVarName = "Apple"
- Pascal Case: MyVarName = "Apple"
- Snake Case: my_var_name = "Apple"
Unpack Collection
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
Output Variables: print()
x = "hello"
y = " world"
print(x)
print(x, y)
print(x + y)
x = 5
y = 10
z = "Apple"
print(x + y)
print(x + z)
print(x, z)
Global & Local Variables
x = "Apple"
def myfunc():
x = "Orange"
print(x + " is delicious")
print(x)
myfunc()
Global Keyword
def myfunc():
global x
x = "Apple"
print(x + " is delicious")
myfunc()
print(x)
Create a Module: .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"])
User Input
username = input("Enter username:")