본문 바로가기

Study Note/Python25

7. F-strings & Try Except F-strings f-strings provide a simple and intuitive way to format strings in Python, making it easier to read and maintain code. To use f-strings, simply prefix the string with 'f' or 'F' and place variable names or expressions inside curly braces {}. The contents within the curly braces are then replaced with the value of the respective variable or expression. F-strings allow variables to be ins.. 2024. 3. 27.
6. Set & tuples and list Set In Python, a set is a data type representing a collection of unique elements. Sets do not allow duplicate elements and are unordered. This makes sets useful for operations such as set union, intersection, and difference. Sets are defined using curly braces {}, with elements separated by commas. For example: my_set = {1, 2, 3, 4, 5} You can create a set from a list or tuple, removing any dupl.. 2024. 3. 27.
5. Uses of [] {} () In Python, `{}` curly braces are used to represent a dictionary. A dictionary is a data structure consisting of key-value pairs. Inside the curly braces, key-value pairs are listed, separated by commas. For example: my_dict = {'name': 'John', 'age': 30, 'city': 'New York'} Here, 'name', 'age', and 'city' are keys, while 'John', 30, and 'New York' are their corresponding values. `()` parentheses .. 2024. 3. 26.
4. Function [Def] Def(ine) The purpose of using Def function. If you frequently use lengthy code that you'd like to abbreviate into a single word, you can do so by defining it as a function. Example 1 def bus_rate (age): if age > 65: print('Its Free') elif age > 20 : print ('Adult') else: print('youth') bus_rate(36) Example 2 def bus_rate (age): if age > 65: return 0 elif age > 20 : return 1200 else: return 750 m.. 2024. 3. 26.
3. IF & loop IF is one of the most commonly used functions as a conditional statement. Here, IF, Else, and spacing are very important. Example money = 3000 if money > 3800: print ('Take taxi!') else : print ('Can not take taxi') print ('what to take?') money = 5000 if money > 3800: print ('Take taxi!') else : print ('Can not take taxi') print ('what to take?') If there are not space money = 5000 if money > 3.. 2024. 3. 25.
2. List & dictionary List The order of values is important. Using the 'list' function allows you to utilize the order of values and the listed words or numbers to access the values. a_list = [1,5,6,3,2] a_list.append(99) a_list.append(992) a_list.append(919) a_list.append(399) print(a_list) a_list = [1,5,6,3,2] result = a_list[:3] print(result) Dictionary It stores values as key-value pairs. a_dict = {'name' : 'bob'.. 2024. 3. 25.