Coding/Basic Skills(Python)

[2차원 배열 / 문자열 / for문] 복습

xeohyuni 2023. 6. 11. 16:30

2D Arrays

  • a data structure that consists of rows and columns, forming a table-like structure. Each element has an index that represents its specific position, which allows us to access the data. 
    • For example 3x3 2d array:
array = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]]

 

  • To access a specific element in an array, you use indices. For instance, array[0][1] refers to the value in the first row and second column. Therefore, in the above array, array[0][1] represents 2.

Strings

  •  a sequence of characters enclosed in single quotes ('') or double quotes (""). Strings are commonly used to represent text or a collection of characters. 
    • For example:
name = "Grace Hong"
message = 'Hello, World!'
  • Strings support various operations and methods that allow you to manipulate and work with them. Here are some common operations and methods used with strings:
  • Length: You can determine the length of a string using the len() function.
message = "Hello, World!"
length = len(message)
print(length)
#output is 13 since it counts starting from 1 and including spaces

 

  • Slicing: You can extract a portion of a string using slicing. Slicing allows you to specify a range of indices to retrieve a substring.
message = "Hello, World!"
substring = message[7:12]
print(substring)  
# Output is World cause it counts from the 7th letter until before 12th letter
  • Indexing:process of accessing individual elements within a sequence, such as strings, lists, or tuples, using their position or index number. The index starts from 0 for the first element and increments by 1 for each subsequent element.
  • You can use square brackets [] after the sequence variable, For exmaple:
my_string = "Hello, World!"
print(my_string[0])  # Output: 'H'
print(my_string[7])  # Output: 'W'
  • You can also use negative indexing for counting it backwards
my_list = [10, 20, 30, 40, 50]
print(my_list[-1])  # Output: 50
print(my_list[-3])  # Output: 30
  • find(): Searches for a substring within a string and returns the index of its first occurrence (find()) 
string = "Hello, World!"
index1 = string.find("World")  # returns 7
  • count(): Returns the number of occurrences of a substring within a string
string = "Hello, World!"
count = string.count("o")  # returns 2

For loop

Used to iterate over a sequence of elements, such as a list, tuple, string, or range. It allows you to execute a block of code repeatedly for each item in the sequence. 

fruits = ['apple', 'banana', 'peach']

for fruit in fruits:
    print(fruit)

The output:

apple
banana
peach

When using a for loop with a range, the loop iterates over a sequence of numbers defined by the range() function. The range() function generates a sequence of numbers based on the provided arguments, which can be used as the sequence for the for loop.

Range finction called in 3 different ways:

1)range(stop)

2) range(start,stop)

3)range(start,stop,step)

------------------------------

1) Example:

for num in range(5):
    print(num)
    
#output:
0
1
2
3
4

2)Example:

for num in range(2, 8):
    print(num)
    
#Ouput
2
3
4
5
6
7

3)Exmaple:

for num in range(0, 10, 2):
    print(num)

#Output:
0
2
4
6
8