Dev/Python

[Coursera] Programming for Everybody (Python) - 2. Expressions, Types

NillK 2015. 8. 31. 01:10

2.1 Expressions

Constants And Variable

  • Constants : 숫자, 문자, 문자열 등 고정된 값. 바뀌지 않는다. 문자열은 ‘ 와 “ 둘 다 쓸 수 있음
  • Variables : 컴퓨터의 메모리에 저장할 수 있는 값. 우리는 이름으로 값을 가져다 쓸 수 있음. 그리고 물론, Constants와 다르게 값을 바꿀 수 있다.

Naming Rules

  • 문자나 _ 로 시작
  • 문자, 숫자, _ 허용
  • Case sensitive

Reserved Words

-

Sentences or Lines

x = 2

Assignment Statement : assign a value to a variable

x = x + 2

Assignment with expression : right side is an expression, after calculate the value and then assign the value to in memory named x vairable

print(x)

Print statement

Numeric Expressions

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • ** : Power
  • % : Remainder

Order of Evalution

  • Python must know which one to do first
  • This is called "operator precedence"
  • Parenthesis -> Power -> Multiplication, Division and Remainder -> Addition and Subtraction -> Left to Right

많은 사람들이 python3를 배우고 가르치고 사용하려고 하는 시기임. 하지만 실제 아직 Python2로 제작되어 있는 것들이 많기 때문에 천천이 바뀌는 중임. (강사님의 사족)

Python Integer Division is Weird

2 버전에서는 다른 언어와 비슷함. 즉 int끼리 나누기 했을 때 소수점 이하는 버리고, 대신 int 나누기 float면 소숫점 나오고



2.2 Type

Python은 똑똑해서 타입을 알 수 있다. 그래서 내가 뭘 의미하는지 알 수 있음.

>>> 1 + 1
2
>>> 'Hello, ' + 'world!'
'Hello, world!'

위의 보기와 같이 + 기호는 둘 다 숫자일 때는 값을 더하고, 둘 다 문자열일 때는 문자열을 이어붙이는 일을 한다. Python이 타입을 알 수 있어야 가능한 이야기.
But! 그래서 타입은 문제를 일으킬 가능성도 존재. Python은 type을 보고 있다는 걸 기억해.
숫자 + 문자 이런 거 안돼!

>>> 1 + 'Hi'
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

숫자 + 문자하면 이런 오류를 만나게 됨.

만약 데이터의 타입을 알고 싶다면?

>>> type('Hello')
<type 'str'>

type function으로 데이터 type 알 수 있음

Numbers have two main types

  1. Integers
  2. Floating Point Numbers

Type Conversions

When you put an integer and floating point in an expression the integer is implicitly converted to a float

String Conversions

int('123') or float('123.01')

User Input

raw_input() pause and read data from the user. raw_input returns a string

(2.xx 기준, 3버전은 그냥 input())

Comments in Python

# -> single line comment

String Operations

  • + : concatenation
  • * : multiple concatenation

Mnmonic Variable Name

Since we programmers are given a choice in how we choose our variable names, there is a bit of best practice
We name variables to help us remember what we intend to store in them (mnemonic = memory aid)
This can confuse begining students because well named variable soften sound so good that they must be keywords
http://en.wikipedia.org/wiki/Mnemonic



2.1.py


2.2.py