728x90

이 글은 Dr. Angela Yu의 [Python_Bootcamp]를 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#Password Generator Project
import random
 
letters = [
    'a''b''c''d''e''f''g''h''i''j''k''l''m''n''o',
    'p''q''r''s''t''u''v''w''x''y''z''A''B''C''D',
    'E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S',
    'T''U''V''W''X''Y''Z'
]
numbers = ['0''1''2''3''4''5''6''7''8''9']
symbols = ['!''#''$''%''&''('')''*''+']
 
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
 
letter_list = [];
for i in range(0, nr_letters):
  random_num = random.randint(0len(letters)-1);
  letter_list.append(letters[random_num]);
 
symbol_list = [];
for i in range(0, nr_symbols):
  random_num = random.randint(0len(symbols)-1);
  symbol_list.append(symbols[random_num]);
 
number_list = [];
for i in range(0, nr_numbers):
  random_num = random.randint(0len(numbers)-1);
  number_list.append(numbers[random_num]);
 
all = letter_list+symbol_list+number_list;
 
random_list = []
 
while len(random_list) < len(all):
    all_len = len(all)
    random_num = random.randint(0, all_len - 1)
    if (random_list.count(random_num) == 0):
        random_list.append(random_num)
 
for i in random_list:
    print(all[i], end="")
 
 
 

1. while len(random_list) < len(all):

    - random_list가 all과 길이가 같아질 때까지 반복

 

2. if (random_list.count(random_num) == 0):

    - count()를 활용하여, 랜덤값 중 중복되지 않은 요소만 random_list에 추가

 

3. for i in random_list:

    - 중복이 제거된 random_list 값을 all에 대입해서 random하게 all list 출력

 

4. print(all[i], end="")

    - end=""를 통해 print 개행 제거

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#Password Generator Project
import random
 
letters = [
    'a''b''c''d''e''f''g''h''i''j''k''l''m''n''o',
    'p''q''r''s''t''u''v''w''x''y''z''A''B''C''D',
    'E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S',
    'T''U''V''W''X''Y''Z'
]
numbers = ['0''1''2''3''4''5''6''7''8''9']
symbols = ['!''#''$''%''&''('')''*''+']
 
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
 
pwd_list = [];
 
for char in range(1, nr_letters + 1):
    random_char = random.choice(letters)
    pwd_list += random_char
 
for char in range(1, nr_symbols + 1):
    pwd_list += random.choice(symbols)
 
for char in range(1, nr_numbers + 1):
    pwd_list += random.choice(numbers)
 
random.shuffle(pwd_list);
 
pwd2 = "";
for char in pwd_list:
  pwd2 += char;
  
print(pwd2);
 
 
 

1. random_char = random.choice(letters)

    - choice(): 리스트에서 랜덤하게 요소를 반환

 

2. list에 요소를 추가하는 방법

    - pwd_list += random_char;

    - pwd_list.append(random_char);

 

3. random.shuffle(pwd_list)

    - shuffle(): 리스트 요소 셔플

 

4. pwd2 += char;

    - StringBuffer와 비슷하게 pwd_list 요소를 pwd2에 추가

 

728x90

'Python > Python' 카테고리의 다른 글

[Python_Bootcamp] Data Structures: List  (0) 2023.08.13
[Python_Bootcamp] f-String  (0) 2023.08.08
728x90

이 글은 Dr. Angela Yu의 [Python_Bootcamp]를 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

Data Structures : List

Main Methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
list1 = ["a""b""c"]
list1.append("d")
# Add an item to the end of the list
print(list1)
# output: ['a', 'b', 'c', 'd']
 
 
list2 = [123]
# list2.extend(4);
# Extend the list by appending all the items from the iterable
# TypeError: 'int' object is not iterable
list2.extend("4")
print(list2)
# output: [1, 2, 3, '4']
 
 
list3 = ["ga""na""ra"]
list3.insert(2"da")
# Insert an item at a given position.
print(list3)
# output: ['ga', 'na', 'da', 'ra']
 
 
list4 = ["do""re""mi"]
list4.remove("re")
# Remove the first item from the list whose value is equal to x.
# list4.remove("pa");
# ValueError: list.remove(x): x not in list
print(list4)
# output: ['do', 'mi']
 
 
list5 = ["a""b""c"]
list5.pop(1)
# Remove the item at the given position in the list, and return it.
# If no index is specified, a.pop() removes and returns the last item in the list.
print(list5)
# output: ['a', 'b']
 
 
list6 = [123]
list6.clear()
# Remove all items from the list.
print(list6)
# output: []
 
 
list7 = ["ga""na""ra""na""da""ma""ba""na"]
index1 = list7.index("na")
# Return zero-based index in the list of the first item whose value is equal to x.
print(index1)
# output: 1
index2 = list7.index("na"2)
# The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list.
print(index2)
# output: 3
# index3 = list7.index("na", 5, 6);
# print(index3);
# ValueError: 'na' is not in list
 
 
list8 = ["do""re""mi""mi"]
mi_cnt = list8.count("mi")
# Return the number of times x appears in the list.
print(mi_cnt)
# output: 2
 
 
list9 = ["1""4""3""5""2"]
list9.sort()
# Sort the items of the list in place.
print(list9)
# output: ['1', '2', '3', '4', '5']
list9.sort(reverse=True)
print(list9)
# output: ['5', '4', '3', '2', '1']
 
 
list10 = ["1""4""3""5""2"]
list10.reverse()
# Reverse the elements of the list in place.
print(list10)
# output: ['2', '5', '3', '4', '1']
 
 
list11 = ["apple""banana""car""door""elephant"];
list12 = list11.copy();
# Return a shallow copy of the list.
print(list12);
# output: ['apple', 'banana', 'car', 'door', 'elephant']
 
 
 

 

 

 

📚 참고 자료

 

5. Data Structures

This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. More on Lists: The list data type has some more methods. Here are all of the method...

docs.python.org

 

728x90

'Python > Python' 카테고리의 다른 글

[Python_Bootcamp] 리스트 랜덤 추출  (0) 2023.08.16
[Python_Bootcamp] f-String  (0) 2023.08.08
728x90

이 글은 Dr. Angela Yu의 [Python_Bootcamp]를 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

f-String

- 문자열 Formatting 기법

- 여러 자료형을 str과 함께 사용할 경우, TypeError 발생

- 여러 자료형을 str로 형변환 해주는 불편함을 줄임

 

1
2
3
4
5
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
 
print(f"your height is {height}m and weight is {weight}");
 
 
 

- {}를 활용하여 변수 삽입, 함수 호출 등을 할 수 있음

 

 

 

📚 참고 자료

 

파이썬의 f-string으로 문자열 포맷팅하기

Engineering Blog by Dale Seo

www.daleseo.com

 

728x90

'Python > Python' 카테고리의 다른 글

[Python_Bootcamp] 리스트 랜덤 추출  (0) 2023.08.16
[Python_Bootcamp] Data Structures: List  (0) 2023.08.13