Skip to content
Snippets Groups Projects
Commit ab07bccb authored by Franziska Niemeyer's avatar Franziska Niemeyer
Browse files

Upload solutions for gap_exercise1_basics

parent d0160d32
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Python course - Exercises Gap text
%% Cell type:markdown id: tags:
Correct the Code
%% Cell type:code id: tags:
```
# What is missing here? Expected output: Hi!
print('Hi!')
```
%% Output
Hi!
%% Cell type:code id: tags:
```
# Here are two mistakes. Can you find them?
# Hi! I am a comment. How are you?
print('Hello world!')
```
%% Output
Hello world!
%% Cell type:code id: tags:
```
# Multi-line comments
"""
I want to be a
mutli line comment.
"""
print('Hello world!')
```
%% Output
Hello world!
%% Cell type:code id: tags:
```
# Expected output: 5
c = 5
print(c)
```
%% Output
5
%% Cell type:code id: tags:
```
# How to print two strings as one. Expected output: Hello world
a = 'Hello '
b = 'world'
print( a + b )
```
%% Output
Hello world
%% Cell type:code id: tags:
```
# Variable types
# Does the error output help you?
a = 'Hello'
b = 0
print( a + str(b) )
```
%% Output
Hello0
%% Cell type:code id: tags:
```
# What is missing?
a = 'Hello'
b = a
if a == b:
print('Same!')
```
%% Output
Same!
%% Cell type:code id: tags:
```
# Print the variable type
# Expected output: a is of type <class 'str'>
a = 'How are you?'
print('a is of type', type(a))
```
%% Output
a is of type <class 'str'>
%% Cell type:code id: tags:
```
# Variable names: What is allowed and what not?
_1th_string = "Hello"
second_string = 'world'
third_string = '!'
FOURTH_STRING = 'How'
FIFTH_STRING = 'are'
_6th_string = 'you?'
print(_1th_string, second_string,
third_string, FOURTH_STRING,
FIFTH_STRING, _6th_string
)
```
%% Output
Hello world ! How are you?
%% Cell type:code id: tags:
```
# convert b into float
a = 2
b = float(a)
print(b)
```
%% Output
2.0
%% Cell type:code id: tags:
```
# How to access Lists?
my_list = ["!", "Hello", 'you', "How", '?', 1, 'are', 2, 3, "world"]
print(my_list)
print(my_list[ 0 ]) # print '!'
print(my_list[ 1 ]) # print 'Hello'
print(my_list[ -1 ]) # print the last element of a list without the real index; here 'world'
### print 'Hello world! How are you?':
print(my_list[ 1 ], my_list[ -1 ] ,my_list[0] ,my_list[ 3 ] ,my_list[ -4 ] ,my_list[ 2 ], my_list[4])
```
%% Output
['!', 'Hello', 'you', 'How', '?', 1, 'are', 2, 3, 'world']
!
Hello
world
Hello world ! How are you ?
%% Cell type:code id: tags:
```
# Indices of Strings
a = 'Hello world!'
print(a[ -1 ]) # only print !
print(a[ 0 ]) # only print H
print(a[ 6:-1 ]) # only print 'world', without '!'
```
%% Output
!
H
world
%% Cell type:code id: tags:
```
# Dictionaries
my_dict = { 'chr1' : 'gene1', 'chr2' : 'gene3' }
print(my_dict[ 'chr1' ]) # print gene1 of the dictionary
```
%% Output
gene1
%% Cell type:code id: tags:
```
# Dictionaries2; Find the mistake
my_dict = { 'chr1' : 'gene1', 'chr2' : 'gene3' }
print(my_dict[ 'chr1' ]) # print gene1 of the dictionary
```
%% Output
gene1
%% Cell type:code id: tags:
```
# Dictionaries3
my_dict = { 'chr1' : ['gene1', 'gene2'], 'chr2' : 'gene3' }
print(my_dict[ 'chr1' ][ 1 ]) # print gene2 of the dictionary
```
%% Output
gene2
%% Cell type:code id: tags:
```
# Booleans
a = 0
b = 1
print(bool( b )) # print True; insert the right variable
print(bool( a )) # print False; insert the right variable
```
%% Output
True
False
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment