Python

[Python] 파이썬 기초 (1)

nock_ji 2024. 4. 2. 02:56

 

message = 'Hello, Python!' #variable = value
print(message)
------------------------------------------------
> Hello, Python!

 

# 변수 이름을 지정할 때의 Rules

  • 문자, 숫자, 밑줄만 사용
    • 문자_숫자; data_1; (o)
    • 숫자_문자; 1_data; (x)
  • 공백 허용 안됨
  • 변수 이름이 길더라도 알아볼 수 있도록 하기 'name' (o) / 'n', 'nm' (x)
  • 소문자 'l'과 대문자 'O'는 되도록 지양, 1과 0과 혼동할 수 있음

# NameError

내가 생성한 변인과 호출하는 변인이 일치하지 않는 경우 

 


# 문자 표현 Method

strings << letter/character variable

use quotation marks, single '', double ""

name = 'rok-ju park'
print(name.title()) #Rok-Ju Park
print(name.upper()) #ROK-JU PARK
print(name.lower()) #rok-ju park

 


# f-string

문자열 맨 앞에 f를 붙이고, {중괄호} 안에 직접 변수 이름이나 출력하고 싶은 것을 넣는다.

f'문자열 {변수} 문자열'

 

[1]

# 문자열 맨 앞에 f를 붙이고, 출력할 변수, 값을 중괄호 안에 넣는다.

s = 'coffee'
n = 5
result = f'저는 {s}를 좋아합니다. 하루 {n}잔 마셔요.'
print(result)
---------------------------------------
저는 coffee를 좋아합니다. 하루 5잔 마셔요.

 

[2]

first_name = 'rok-ju'
last_name = 'park'
full_name = f'{first_name} {last_name}'
print(full_name.title())
---------------------------------------
Rok-Ju Park

 

https://blockdmask.tistory.com/429

 

[python] 파이썬 f-string (문자열 포매팅 방법 3)

안녕하세요. BlockDMask 입니다. 오늘은 파이썬 문자열 포매팅 방법 % 서식문자, str.format, f-string 이 세개 중 마지막인 f-string에 대해서 알아보려고 합니다.% 서식문자 [바로가기] str.format [바로가기]

blockdmask.tistory.com


# Tab 탭 추가 \t

#adding space
#tap
print('python')
print('\tPython') #tap 기능: \t
-------------------------------
python
	Python

 

 

# 새로운 줄 만들기 \n

#add new lines
print('Languages:\nPython\nR\nC++\nJavaScript')
------------------------------------------------
Languages
Python
R
C++
JavaScript

 

 

# 공백 없애기

favorite_language = ' python '
favorite_language.rstrip() #strip space on the right side
favorite_language.lstrip() #strip space on the left side
favorite_language.strip()
-----------------------------------------------
' python '
' python'
'python '
'python'

 

# SyntaxError

문자열이 포함된 구문 오류
인용구가 있을 때 따옴표를 사용 X

 

# Numbers

#interger << 정수
#can do +, -. *, /
2 * 3 #6

#** = exponent << 지수
10 ** 6 #1000000

#floats: number with a decimal point
0.1 + 0.2 #some wired arbitrary number > 0.300000000000004

#always returns floats after division
4 / 2 #2.0
1 + 2.0 #3.0

#use underscores for large numbers
earth_age = 14_000_000_000 #14000000000
earth_age = 140_00_00_00_00 #14000000000

 

 

# Multiple Assignments

x, y, z = 0, 1, 2

 

 

'Python' 카테고리의 다른 글

[Python] 파이썬 기초(2)  (0) 2024.04.02
[Python] 파이썬 개발 환경 Google Colab 구글 코랩  (0) 2024.03.29