본문 바로가기
HOO's LIFE/Q&A

[Python Q&A] Lab session for variables, operators, expressions, basic I/O, and string manipulation

by Henry Cho 2022. 9. 20.
728x90

Lab session for variables, operators, expressions, basic I/O, and string manipulation


 

# Exercise 01

print('Sup World?')
print(3 + 2 )
print('3 + 2')
MyNumber = 3+2.0
MyName = 'Dusty'
print(MyName, MyNumber)
print('MyName', 'MyNumber')

Sup World?
5
3 + 2
Dusty 5.0
MyName MyNumber

What is the difference between print(3 + 2) and print('3 + 2') ?

위의 코드에서 3 + 2 를 출력하면 5가 나오지만 '3+2'를 출력하면 3+2가 산출된다. 3+2의 경우에는 수학적 표현에 해당되기 때문에 5라는 산술적 결괏값이 나오며, data type으로는 Integer에 해당된다. 반면에 '3+2'의 경우에는 수학적 표현이 아닌 문자열로 인식되기 때문에 data type 또한 Integer가 아닌 String에 해당된다. 따라서 '3+2'는 하나의 문자로 인식하여 최종 산출되는 값이 5가 아닌 하나의 문자열인 3+2가 산출된다.

 

What is the difference between print(MyName, MyNumber) and print('MyName', 'MyNumber') ?

MyName과 MyNumber는 각각 변수값을 가지고 있는 변수명에 해당한다. 따라서 첫 번째 경우에는 변수명에 맞는 변수값이 출력된다. 반면에 'MyName, 'MyNumber'의 경우에는 String으로 인식되기 때문에 별도의 값이 아니라 문자에 해당하는 MyName MyNumber가 출력되는 것이다. 한마디로 변수명과 동일하다고 하지만 data type이 다르기에 ' ' 차이로 인해 다른 값으로 인식되는 것이다.

 

Change MyNumber = 3+2 to MyNumber = 3+2.0 , and re-run the script, what happens? Why?

숫자의 경우에는 소숫점 자리에 따라서 data type이 다르다. 파이썬의 경우에는 별도의 data type 지정 없이도 소수점을 표기했는지에 따라서 data type이 달라질 수 있다. 3+2의 경우에는 Integer에 해당하기에 최종 산출되는 값 또한 Integer type인 5만 산출된다. 반면에 3+2.0의 경우에는 소수점 자리를 포함하기 있기에 최종 값으로 5.0으로 출력되며, 이는 data type에서 float에 해당한다고 볼 수 있다.


# Exercise 02

Create a simple script that illustrates the following types of variables integer floating point (real) string (alphanumeric) boolean by

1. Assigning a value to a string, integer, float, and boolean variable. Use meaningful names for each variable.

2. Print the contents of each variable.

# String value
introduce = "Nice to meet you!"
welcome = "Hello" + " " + "Nice to meet you!" 

# Int value
age = 21

# Float value
grade = 97.9

print(introduce)
print(welcome)
print(age)
print(grade)
print(bool(grade))
print("This is exercise " + str(1+1) + " " + "and it is " + str(True))

val = True
if bool(val) == True:
  print("True")
else:
  print("False")

Nice to meet you!
Hello Nice to meet you!
21
97.9
True
This is exercise 2 and it is True
True

# Exercise 03

Run the code cell below.

Now, calculate the expressions in the code cell below by hand and see if your output matches the Python output.

 

x1 = 7 + 3 * 6 / 2 - 1
x2 = 2 % 2 + 2 * 2 - 2 / 2
x3 = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) )
print("x1:", x1)
print("x2:", x2)
print("x3:", x3)

# Exercise 04

Get two floating point numbers via the input() function and store them under the variable names 'float1' and 'float2'. Print 'float1' and 'float2' to the output screen. Following this, check whether 'float1' is greater than or equal to 'float2'.

 

input_float1 = float(input("Enter a float: "))
float1 = "{:.2f}".format(input_float1)
input_float2 = float(input("Enter a float: "))
float2 = "{:.2f}".format(input_float2)

print(float1)
print(float2)

if float1 > float2:
  print('float1 is greater than float2.')
elif float1 == float2:
  print('float1 is eqaul float2')
else:
  print('float2 is greater than float1')

Enter a float: 22
Enter a float: 33
22.00
33.00
float2 is greater than float1

# Exercise 05

Define the string given below in quotes to a meaningul variable name.

'Computational Thinking' After initializing the variable:

 

1. Index and print all the elements from index positions 2 to 10.

2. Index and print the string 'Think' from 'Computational Thinking'.

CT = [ 'C', 'o', 'm', 'p', 'u', 't', 'a', 't', 'i', 'o', 'n', 'a', 'l', ' ', 'T', 'h', 'i', 'n', 'k', 'i', 'n', 'g']

print(CT[2:10])
print(CT[14:22])

['m', 'p', 'u', 't', 'a', 't', 'i', 'o']
['T', 'h', 'i', 'n', 'k', 'i', 'n', 'g']

참고로 Exercise 05에서 요구하는 답변이 리스트로 나타내길 원치 않는다면 String만으로만 작성해주면 된다.

CT = "Computational Thinking"

print(CT[2:10])
print(CT[14:19])

mputatio
Think

 

728x90

댓글