Docsity

Basic Concepts of Computing: String Concatenation, Type Conversion, Lists, and Loops, Study notes of Forensics

Toggle sidebar
bg1
ECS 10 - Basic Concepts of Computing
Disc. #2 TA: Yu Wang ([email protected])
04-11-07
- 1 -
* You can find more examples on the web
** You can find all my example scripts on my page: wwwcsif.cs.ucdavis.edu/~wang2/cs10.html
Discussion Notes*
1. String concatenation
String can be constructed by concatenating substrings together. We can use arithmetic +
symbol to do this task, and the resultant string can be the “sum” of as many substrings as we
supply. Let’s see a few examples:
>>> print "ECS " + "10 " + "is " + "fun"
ECS 10 is fun
>>> substr1 = "ECS "
>>> substr2 = "10 "
>>> substr3 = "is "
>>> substr4 = "fun "
>>>
>>> print substr1 + substr2 + substr3 + substr4
ECS 10 is fun
>>>
>>> # But we cannot subtract strings
>>> myString = substr1 + substr2 + substr3 + substr4
>>> print myString
ECS 10 is fun
>>> myString - "ECS "
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
myString - "ECS "
TypeError: unsupported operand type(s) for -: 'str' and 'str'
2. Type Conversion
We already learned that variables can be used to store values of certain type. It is sometimes
necessary to convert variables from one type into another. For example, if you need to change
some of the characters of a string, you will have to transform the string in a mutable list (Prof.
Ludaecher will cover the concept mutable vs. immutable later in the quarter). The following
table provides a list of possible type conversions.
Function Description
int(x) Converts x to an integer
float(x)
Converts x to a floating-point number
str(x) Converts x to a string representation
long(x) Converts x to a long integer
chr(x) Converts integer x to a character
pf3
pf4
pf5

Partial preview of the text

Download Basic Concepts of Computing: String Concatenation, Type Conversion, Lists, and Loops and more Study notes Forensics in PDF only on Docsity!

Disc. # 04-11-

  • 1 -
  • You can find more examples on the web

Discussion Notes*

1. String concatenation

String can be constructed by concatenating substrings together. We can use arithmetic + symbol to do this task, and the resultant string can be the “sum” of as many substrings as we supply. Let’s see a few examples:

>>> print "ECS " + "10 " + "is " + "fun" ECS 10 is fun >>> substr1 = "ECS " >>> substr2 = "10 " >>> substr3 = "is " >>> substr4 = "fun " >>> >>> print substr1 + substr2 + substr3 + substr ECS 10 is fun >>> >>> # But we cannot subtract strings >>> myString = substr1 + substr2 + substr3 + substr >>> print myString ECS 10 is fun >>> myString - "ECS " Traceback (most recent call last): File "", line 1, in myString - "ECS " TypeError: unsupported operand type(s) for -: 'str' and 'str'

2. Type Conversion

We already learned that variables can be used to store values of certain type. It is sometimes necessary to convert variables from one type into another. For example, if you need to change some of the characters of a string, you will have to transform the string in a mutable list (Prof. Ludaecher will cover the concept mutable vs. immutable later in the quarter). The following table provides a list of possible type conversions.

Function Description int(x) Converts x to an integer float(x) Converts x to a floating-point number

str(x) Converts x to a string representation long(x) Converts x to a long integer chr(x) Converts integer x to a character

Disc. # 04-11-

  • 2 -
  • You can find more examples on the web

Let’s see a few examples:

3. List

3.1 What is list in Python?

There are six sequence types in Python, and list is one of them. Sequence can be thought of as a data structure where you can store large and related data for future reference. List is a mutable sequence; by mutable I mean you can alter the content of a list. You will see immutable sequences in Prof. Ludaescher’s lecture later.

3.2 How to construct a list and what to do with it: syntax + examples

Lists are constructed with square brackets [ ], separating items with commas: [a, b, c] is a list of three characters. We can do lots of operations on list, and here are some interesting ones:

Disc. # 04-11-

  • 4 -
  • You can find more examples on the web

4.2 How to understand the above example: syntax

So, you need to give a variable name, x in this example, a list of things, (1, 2, 3, 4, 5) in this example, and some stuff to do once for each item in the list. The “stuff” here is called the loop body. The list doesn’t have to be written as (1, 2, 3, 4, 5), you can also use [ ]. See the following example:

>>> for x in [1,2,3,4,5]: print x,

1 2 3 4 5 >>>

Why both () and [] work? We will look at this later in the quarter, for now it is sufficient to remember that you can use both to write out a list (although we cannot use the term list for both notations)

4.3 More repetitions? Use range()

Often you just want to do something 5 times, so you could say for x in 1,2,3,4,5: but what if you wanted 2000 repetitions? Use your friend range()

>>> print range(5) [0, 1, 2, 3, 4] >>> >>> for x in range(20): print x,

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 >>>

So range(n) will give a list of n numbers starting from 0, ending with n-1. With the help of range(), we can have our for loop to repeat executing its body for as many times as required.

Disc. # 04-11-

  • 5 -
  • You can find more examples on the web

4.4 Loop example: print out the doubled value of the first 20 numbers

5. An example script that covers all topics of today’s discussion