• Keine Ergebnisse gefunden

Basics of CS

N/A
N/A
Protected

Academic year: 2022

Aktie "Basics of CS"

Copied!
70
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Departement Informatik

Basics of CS

Lists, strings, loops

Hans-Joachim Böckenhauer Dennis Komm

Autumn 2021 – October 14, 2021

(2)

Output

(3)

The Function print()

Output on screen with print()

Default: Linebreak after output Linebreak can be replaced by end

print("The value of x is", end=" ") print(x)

Multiple parameters also separated by comma

print("Two times the value of", x, "is", 2 * x)

Default: Whitespace between strings Whitespace can be replaced by sep

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 1 / 21

(4)

The Function print()

Output on screen with print() Default: Linebreak after output

Linebreak can be replaced by end

print("The value of x is", end=" ") print(x)

Multiple parameters also separated by comma

print("Two times the value of", x, "is", 2 * x)

Default: Whitespace between strings

Whitespace can be replaced by sep

(5)

The Function print()

Output on screen with print() Default: Linebreak after output

Linebreak can be replaced by end

print("The value of x is", end=" ") print(x)

Multiple parameters also separated by comma

print("Two times the value of", x, "is", 2 * x)

Default: Whitespace between strings Whitespace can be replaced by sep

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 1 / 21

(6)

The Function print()

Output on screen with print() Default: Linebreak after output

Linebreak can be replaced by end

print("The value of x is", end=" ") print(x)

Multiple parameters also separated by comma

print("Two times the value of", x, "is", 2 * x)

(7)

User Input

(8)

User Input

Variables

Variables are “containers” for values So far values have been fixed in program

name = "Brunhold"

print("Hello", name)

In the real world, values are mostly entered by the user

read in from file / data base

(later)

(9)

User Input

Variables

Variables are “containers” for values So far values have been fixed in program

name = "Brunhold"

print("Hello", name)

In the real world, values are mostly entered by the user

read in from file / data base

(later)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 2 / 21

(10)

User Input

Variables

Variables are “containers” for values So far values have been fixed in program

name = "Brunhold"

print("Hello", name)

In the real world, values are mostly entered by the user

read in from file / data base (later)

(11)

Celsius to Fahrenheit Calculator

User input with function input()

name = input("Enter your name: ") print("Hello", name)

Attention

Input is string (possibly made of digits) and no number

x = input("Enter a number: ") output = 3 * x

print(output) # String concatenation instead of multiplication

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 3 / 21

(12)

Celsius to Fahrenheit Calculator

User input with function input()

name = input("Enter your name: ") print("Hello", name)

Attention

Input is string (possibly made of digits) and no number

x = input("Enter a number: ")

(13)

Celsius to Fahrenheit Calculator

To get a number, the input has to be converted using the function int()

x = input("Enter a temperature in degree Celsius: ") celsius = int(x)

fahrenheit = 9 * celsius / 5 + 32

print("The temperature in degree Fahrenheit is", fahrenheit)

or shorter. . .

celsius = int(input("Enter a temperature in degree Celsius: ")) fahrenheit = 9 * celsius / 5 + 32

print("The temperature in degree Fahrenheit is", fahrenheit)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 4 / 21

(14)

Celsius to Fahrenheit Calculator

To get a number, the input has to be converted using the function int()

x = input("Enter a temperature in degree Celsius: ") celsius = int(x)

fahrenheit = 9 * celsius / 5 + 32

print("The temperature in degree Fahrenheit is", fahrenheit)

or shorter. . .

celsius = int(input("Enter a temperature in degree Celsius: "))

fahrenheit = 9 * celsius / 5 + 32

(15)

Simple Loops

(16)

for -Loops

Repeat a statement block a specific number of times

A for-loop consists of the following parts

A control variable, which is defined in the loop head Additionally, a range is specified

In the loop body, the control variable consecutively takes all values in the

provided range

(17)

for -Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts

A control variable, which is defined in the loop head Additionally, a range is specified

In the loop body, the control variable consecutively takes all values in the provided range

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 5 / 21

(18)

for -Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts

A control variable, which is defined in the loop head

Additionally, a range is specified

In the loop body, the control variable consecutively takes all values in the

provided range

(19)

for -Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts

A control variable, which is defined in the loop head Additionally, a range is specified

In the loop body, the control variable consecutively takes all values in the provided range

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 5 / 21

(20)

for -Loops

Repeat a statement block a specific number of times A for-loop consists of the following parts

A control variable, which is defined in the loop head Additionally, a range is specified

In the loop body, the control variable consecutively takes all values in the

provided range

(21)

for -Loops

for i in range(1, 4):

print(”Test”) print(i)

Attention

Last value of i is 3 and not 4

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 6 / 21

(22)

for -Loops

for i in range(1, 4):

print(”Test”) print(i)

Loop head

Attention

Last value of i is 3 and not 4

(23)

for -Loops

for i in range(1, 4):

print("Test") print(i)

Loop body (indented)

Attention

Last value of i is 3 and not 4

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 6 / 21

(24)

for -Loops

for i in range(1, 4):

print(”Test”) print(i)

Control variable

Attention

Last value of i is 3 and not 4

(25)

for -Loops

for i in range(1, 4):

print(”Test”) print(i)

Range

Attention

Last value of i is 3 and not 4

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 6 / 21

(26)

for -Loops

for i in range(1, 4):

print(”Test”) print(i)

Range

Attention

Last value of i is 3 and not 4

(27)

for -Loops

for i in range(1, 4):

print("Test") print(i)

results in. . .

print("Test") print(1)

print("Test") print(2)

print("Test") print(3)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 7 / 21

(28)

for -Loops

for i in range(1, 4):

print("Test") print(i)

results in. . .

print("Test") print(1)

print("Test") print(2)

print("Test")

print(3)

(29)

for -Loops

for i in range(1, 4):

print("Test") print(i)

results in. . .

print("Test") print(1)

print("Test") print(2)

print("Test") print(3)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 7 / 21

(30)

for -Loops

for i in range(1, 4):

print("Test") print(i)

results in. . .

print("Test") print(1)

print("Test") print(2)

print("Test")

print(3)

(31)

for -Loops

for i in range(1, 4):

print("Test") print(i)

results in. . .

print("Test") print(1)

print("Test") print(2)

print("Test") print(3)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 7 / 21

(32)

Exercise – Square Numbers

Write a program that

gets an integer from the user

stores the value in a variable x

outputs the first x square numbers

(starting with 1)

(33)

Exercise – Square Numbers

x = int(input("Input: ")) for i in range(1, x+1):

print(i * i)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 9 / 21

(34)

Nested Loops

(35)

Nested Loops

Program

for i in range(0, 10):

for j in range(0, 10):

print(j, end=" ") print()

Output

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 10 / 21

(36)

Nested Loops

Program

for i in range(0, 10):

for j in range(0, 10):

print(j, end=" ") print()

Output

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

(37)

Exercise – Number Triangle

Write a program that generates the following output:

0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 3 4 5 6 7 8 9 4 5 6 7 8 9 5 6 7 8 9 6 7 8 9 7 8 9 8 9 9

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 11 / 21

(38)

Exercise – Number Triangle

for i in range(0, 10):

for j in range(i, 10):

print(j, end=" ")

print()

(39)

Lists

(40)

Lists

Simple access to related data

Example

data = [5, 1, 4, 3]

Every element has an index, starting with 0

Access (“read/write”) to single element using brackets data[0] = 5

Length of the list = Number of elements

len(data) = 4

(41)

Lists

Simple access to related data Example

data = [5, 1, 4, 3]

Every element has an index, starting with 0

Access (“read/write”) to single element using brackets data[0] = 5

Length of the list = Number of elements len(data) = 4

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 13 / 21

(42)

Lists

Simple access to related data Example

data = [5, 1, 4, 3]

Every element has an index, starting with 0

Access (“read/write”) to single element using brackets data[0] = 5

Length of the list = Number of elements

len(data) = 4

(43)

Lists

Simple access to related data Example

data = [5, 1, 4, 3]

Every element has an index, starting with 0

Access (“read/write”) to single element using brackets data[0] = 5

Length of the list = Number of elements len(data) = 4

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 13 / 21

(44)

Lists

Simple access to related data Example

data = [5, 1, 4, 3]

Every element has an index, starting with 0

Access (“read/write”) to single element using brackets data[0] = 5

Length of the list = Number of elements

(45)

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3] for item in data:

print(item)

or alternatively. . .

data = [5, 1, 4, 3]

for i in range(0, len(data)): print(data[i])

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 14 / 21

(46)

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3]

for item in data:

print(item)

or alternatively. . .

data = [5, 1, 4, 3]

for i in range(0, len(data)):

print(data[i])

(47)

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3]

for item in data:

print(item)

Takes values of all elements in list

or alternatively. . .

data = [5, 1, 4, 3]

for i in range(0, len(data)): print(data[i])

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 14 / 21

(48)

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3]

for item in data:

print(item)

Takes values of all elements in list

or alternatively. . .

data = [5, 1, 4, 3]

for i in range(0, len(data)):

(49)

Loops over Lists

Output all elements of list

data = [5, 1, 4, 3]

for item in data:

print(item)

Takes values of all elements in list

or alternatively. . .

data = [5, 1, 4, 3]

for i in range(0, len(data)):

print(data[i])

Takes values of all indices of elements in list

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 14 / 21

(50)

Loops over Lists – The Function reversed()

Output all elements of list backwards

data = [6, 7, 5, 1]

for item in reversed(data): print(item, end=" ")

or alternatively. . .

for i in range(len(data)-1, -1, -1): print(data[i], end=" ")

Result

1 5 7 6

(51)

Loops over Lists – The Function reversed()

Output all elements of list backwards

data = [6, 7, 5, 1]

for item in reversed(data):

print(item, end=" ")

or alternatively. . .

for i in range(len(data)-1, -1, -1): print(data[i], end=" ")

Result 1 5 7 6

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 15 / 21

(52)

Loops over Lists – The Function reversed()

Output all elements of list backwards

data = [6, 7, 5, 1]

for item in reversed(data):

print(item, end=" ")

or alternatively. . .

for i in range(len(data)-1, -1, -1):

print(data[i], end=" ")

Result

1 5 7 6

(53)

Loops over Lists – The Function reversed()

Output all elements of list backwards

data = [6, 7, 5, 1]

for item in reversed(data):

print(item, end=" ")

or alternatively. . .

for i in range(len(data)-1, -1, -1):

print(data[i], end=" ")

Result 1 5 7 6

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 15 / 21

(54)

Simple Initialization

Initialize list with same value in all cells

data = [0] * 1000 print(data)

data = [0, 0, 0, 0] * 250 print(data)

Result

[0, 0, 0, 0, 0, 0, 0, ..., 0] (1000 zeros)

(55)

Simple Initialization

Initialize list with same value in all cells

data = [0] * 1000 print(data)

data = [0, 0, 0, 0] * 250 print(data)

Result

[0, 0, 0, 0, 0, 0, 0, ..., 0] (1000 zeros)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 16 / 21

(56)

Simple Initialization

Initialize list with same value in all cells

data = [0] * 1000 print(data)

data = [0, 0, 0, 0] * 250 print(data)

Result

[0, 0, 0, 0, 0, 0, 0, ..., 0] (1000 zeros)

(57)

Simple Initialization

Initialize list with same value in all cells

data = [0] * 1000 print(data)

data = [0, 0, 0, 0] * 250 print(data)

Result

[0, 0, 0, 0, 0, 0, 0, ..., 0] (1000 zeros)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 16 / 21

(58)

The Function append()

Add element to end of a list

Example List data

data.append(5) inserts 5 at the end

data = [1, 4, 8] data.append(9) data.append(14)

print(data)

(59)

The Function append()

Add element to end of a list Example

List data

data.append(5) inserts 5 at the end

data = [1, 4, 8] data.append(9) data.append(14)

print(data)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 17 / 21

(60)

The Function append()

Add element to end of a list Example

List data

data.append(5) inserts 5 at the end

data = [1, 4, 8]

data.append(9)

data.append(14)

(61)

Exercise – Initialize List

Write a program that

gets an integer from the user stores the value in a variable x initializes a list with the first x even numbers (starting at 0)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 18 / 21

(62)

Exercise – Initialize List

x = int(input("Input: ")) data = []

for i in range(0, x):

data.append(2 * i)

print(data)

(63)

Merging lists

Merge lists using +

Example List data

data + [1, 2] adds elements 1 and 2 at the end

data = [4, 6, 7] data2 = data + [9]

data2 = [1, 3] + data2 + [12, 14]

print(data2)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 20 / 21

(64)

Merging lists

Merge lists using + Example

List data

data + [1, 2] adds elements 1 and 2 at the end

data = [4, 6, 7] data2 = data + [9]

data2 = [1, 3] + data2 + [12, 14]

print(data2)

(65)

Merging lists

Merge lists using + Example

List data

data + [1, 2] adds elements 1 and 2 at the end

data = [4, 6, 7]

data2 = data + [9]

data2 = [1, 3] + data2 + [12, 14]

print(data2)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 20 / 21

(66)

Strings

(67)

Strings

Strings are “lists of characters” (there are differences)

Characters correspond (mostly) to keys on keyboard Strings are written in quotation marks

Access to single characters using brackets (“read-only”)

String word = "HELLO WORLD" word[0] is first character word[1] is second character . . .

word[len(word)-1] is last character (word[-1], respectively)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 21 / 21

(68)

Strings

Strings are “lists of characters” (there are differences) Characters correspond (mostly) to keys on keyboard

Strings are written in quotation marks

Access to single characters using brackets (“read-only”)

String word = "HELLO WORLD" word[0] is first character word[1] is second character . . .

word[len(word)-1] is last character (word[-1], respectively)

(69)

Strings

Strings are “lists of characters” (there are differences) Characters correspond (mostly) to keys on keyboard

Strings are written in quotation marks

Access to single characters using brackets (“read-only”) String word = "HELLO WORLD"

word[0] is first character word[1] is second character . . .

word[len(word)-1] is last character (word[-1], respectively)

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 21 / 21

(70)

Thanks for your attention

Referenzen

ÄHNLICHE DOKUMENTE

Basics of Computer Science for Human Medicine – Introduction to the Course Autumn 2021 Böckenhauer, Komm 1 / 36.?.

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 1 / 21.

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 1 / 36... Exercise –

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 7 / 36.

Basics of Computer Science for Human Medicine – Functions and return values Autumn 2021 Böckenhauer, Komm 2 / 26... but can in general not be

Basics of Computer Science for Human Medicine – Functions and return values Autumn 2021 Böckenhauer, Komm 1 / 26.

Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 7 / 28... Then the left algorithm is faster ï Loop is left after

Basics of Computer Science for Human Medicine – numpy, matplotlib Autumn 2021 Böckenhauer, Komm 4 /