1. Tuple
A tuple is an immutable collection with ordered elements that can store values of various data types.
- Immutable: After creating tuples, elements cannot be added, deleted, or modified.
- Efficient: Tuples are more memory-efficient than lists, particularly aiding in performance enhancement when data is infrequently modified.
1-1. Creation
Tuple is created using parentheses (), and each item is separated by commas.
1-2. Indexing
- Each item in a tuple has a position (index) and can be accessed using the index.
- Indexing starts from 0.
1-3. Slicing
- Only a portion of the tuple can be extracted through slicing.
1-4. Operation
- Tuples can be combined using the + operation between tuples..
- User can iterate over a tuple with the * operation.'
1-5. Unpacking
- Tuple items can be assigned to variables. (Also possible with lists)
1-6. Membership Testing
- The in operator can be used to check if a specific value exists in a tuple. (Also possible with lists)
1-7. Sorting Tuples
- Tuples don't provide a sort() method, but they can be sorted using the sorted() function.
1-8. Conversion
- Tuples can be converted to lists and vice versa.
2. Input Function
- Function that can receive data input from the user (developer)
- Data is always entered as a string (str) type.
※. Question:
Write a program that calculates the total and average scores for receiving Korean, English, and math scores.
However, the input method is in the form of 100/70/85
Answer:
Kor, Eng, Math = input('Please enter your Kor, Eng, and Math scores separated by a slash (/).').split('/')
# The First Anwer
a=(int(Kor) + int(Eng)+ int(Math))
b=a/3
print("Total_Score:", a)
print("AVG:", b)
print('-----------')
# The Second Anwer
print(f'Total_Score: {int(Kor)+int(Eng)+int(Math)}')
print(f'AVG: {(int(Kor)+int(Eng)+int(Math))/3}')
print(f'AVG: %.2f' % ((int(Kor)+int(Eng)+int(Math))/3))
Result:
'Python(Eng. ver)' 카테고리의 다른 글
7. Python collection type - Dictionary (0) | 2024.03.14 |
---|---|
6. Python collection type - Set (0) | 2024.03.14 |
4. Python collection type - List (0) | 2024.03.14 |
3. Python String & why 0.1+ 1.1 ≠ 1.2 (0) | 2024.03.13 |
2. Variables in Python (0) | 2024.03.13 |