CS计算机代考程序代写 python CS1-FULL-FALL2021

CS1-FULL-FALL2021

for

Syntax…

for a_variable in set_of_values:
# code block of for

while condition:
# code block of the while

Value is a *variable* declared in the cycle for,
In each iteration (cycle) it takes a successive value in
the set of elements specified after the word in

Result

The same in another way…

The list can be inside a variable …

What prints this program?

A.None

B.[123, 345, 456, 567]

C.[61.5, 172.5, 228.0, 283.5]

What prints this program?

A.None

B.[123, 345, 456, 567]

C.[61.5, 172.5, 228.0, 283.5]

The lists are types “by reference”, that is, by
passing them to a function as a parameter,
the function receives a reference and not a
copy. If the function modifies the list, then
the list is modified after the function has

been executed.

!

How can we access randomly an
element of a list?

?

Strings

Strings

• In Python a string is a list with characters

• So we can use the concept of a list to find patterns on a
string or to find words on a big string

The split() function

• In many situations, we need to divide a string into
chunks.

• For example, if we have a list of names in a string:

• ”John, Mary, Mike, Gabriel, Bill”

• How do we transform this string into a list and access
the fourth name (Gabriel)?

• split() – returns a list with the words of the original
substring that are delimited by spaces

• split(separator) – returns a list with the words of the
original substring that are delimited by the substring
separator

The split() function

• ”John, Mary, Mike, Gabriel, Bill”

• How do we transform this string into a list and access
the fourth name (Gabriel)?

• We use the function split () to get a list with the
individual names.

It’s fine, but it’s
not exactly what

we want

split () is
separating

the elements
considering

the spaces as
separators

How do we remove the commas?

?

The problem is that
now it leaves us

spaces

Unlike the previous
case, we specify that

the list is delimited by a
comma (,)

How do we remove the spaces?

?

The strip() function

The strip () function returns
the clean string of spaces on

the left and on the right

We keep the coma a
separator from the list

Let’s review the
Functions:

• find(x) – delivers the value of the index that the substring occupies
equal to x within the string

• find(x, start) – gives the value of the index that occupies the
substring equal to x within the string, looking from the start
position onwards

• lower() – returns a copy of the original string with all the letters in
lower case, it does not modify the original string

• upper() – returns a copy of the original string with all the letters in
upper case, it does not modify the original string

• replace(old, new) – returns a copy of the original string with all the
letters in lower case, it does not modify the original string

Dividing a list (sublist)

• We can divide any list in any size
• To do that we use the square brackets [] an the : to say the data we

want

y = [99, 999, 77, 777, 88, 888]
x= y[0 : 3]

x = [99, 999, 77]
a = y[2 : len(y)]

a = [77, 777, 88, 888]