The best way to learn Python is to practice, practice, practice. That’s why we’re sharing this post: so you can test out your basic Python skills with these six challenges. These exercises are useful for everyone, especially if you’re a beginner with basic knowledge of Python concepts.

Your challenge is to write a Python program that prints the following messages.

Hello World

Let’s Learn Python

Sincerely, (your name here)

Each of the three inputs should print on a new line.

Hint:

```
print () # what is the message to be printed?
print ()
print ()
```

Solution:

```
print ("Hello World")
print ("Let's Learn Python")
print ("Sincerely, the Educative Team")
```

*Explanation of Challenge #1*

First, we use the `print`

statement, which will display the result. We type our text within the `( )`

. We have to surround our text with “quotations marks” since it is a string. To start our second line of text, we hit enter and repeat this process on line 2 and 3 because each call to `print`

will move the output to a new line. If you’re dealing with numerical input, you won’t need to add quotation marks.

*Quick tip:* For strings, you can use either double quotation marks (" ") or single (’ '). Double quotation marks should be used if your sentence or word makes use of an apostrophe. In our example, “Let’s learn Python”, if you were to use single quotes you’d receive an error.

Your challenge is to write a Python program that calculates and prints the gravitational force between the Earth and the Sun using the `grav_force`

variable. The formula for gravitational force is as follows:

*F = GMm / r ^{2}*

`F`

is the total gravitation force. `G`

is the gravitational constant. `M`

and `m`

are the masses to be compared, and `r`

is the distance between those masses.

The values you will need to calculate the gravitation force of the Earth and Sun are offered below.

- G = 6.67 * 10
^{-11} - Msun = 2.0 * 10
^{30} - mEarth = 6.0 * 10
^{24} - r = 1.5 * 10
^{11}

Hint:

```
G = 6.67 * (10 ** -11)
M = # what is the Mass of the Sun?
m = # what is the mass of the Earth
r = 1.5 * (10 ** 11)
# how do we calcuate grav force?
# how do we display the answer?
```

Solution:

```
G = 6.67 * (10 ** -11)
M = 2.0 * (10 ** 30) # Mass of Sun
m = 6.0 * (10 ** 24) # Mass of Earth
r = 1.5 * (10 ** 11)
grav_force = (G * M * m) / (r ** 2)
print (grav_force)
```

*Explanation of Challenge #2*

This challenge is actually simpler than it seems! Don’t let the big numbers concern you. We use our arithmetic operators to perform all the operations. The `*`

operator multiplies variables, the `\`

operator divides variables, and the `**`

performs an exponent operation.

First, we define each of our values (G, M, m, r). This stores them in the `grav_force`

variable. We then define the `grav_force`

equation using Python syntax and ask the program to print the answer. The parentheses are actually optional: we just used them to separate the top and bottom of the fraction.

*Fun fact: the gravitational force between the Earth and Sun is approximately 3,000x the weight of the Earth’s oceans combined!*

Your challenge is to write a Python program that calculates the discounted price of an object using the `if-elif-else`

statement.

Your conditions are as follows:

- If the price is 300 or above, it will be discounted by 30%
- If the price falls between 200 and 300, it will be discounted by 20%
- If the price falls between 100 and 200, it will be discounted by 10%
- If the price is less than 100, it will be discounted by 5%
- There is no discount for negative prices

Your input:

```
price = 350
```

Hint:

```
price = # what is the input price?
if (price >= 300):
price *= # what are the conditions?
elif ( )
price *=
elif ( )
price *=
elif ( )
price *=
else:
?
# how do we display the answer?
```

Solution:

```
price = 350
if (price >= 300):
price *= 0.7 # (1 - 0.3)
elif (price >= 200):
price *= 0.8 # (1 - 0.2)
elif (price >= 100):
price *= 0.1 # (1 - 0.1)
elif (price >= 50):
price *= 0.05 # (1 - 0.05)
else:
price
print (price)
```

*Explanation of Challenge #3*

To complete this challenge, we first define the price and then all our conditions using Python syntax. You only need to specify the lowest number of each condition since the higher number is accounted for in the previous condition. We calculate a discounted price with `price * (1 - discount)`

.

We then ask the program to `print`

our discounted price.

Your challenge is to write a Python program using the `rep_cat`

function, which takes two integers (`x`

and `y`

) and converts them into strings. The string value of `x`

should repeat 8 times, and the string value of `y`

should repeat 5 times. The `y`

string must then be concatenated to the `x`

string and returned as a single piece of data.

Your inputs:

```
x = 7
y = 2
```

Hint:

```
def rep_cat# what are the integers?
return str(x) * ? + str(y) * ? # what are the inputs?
# how do we display the answer?
```

Solution:

```
def rep_cat(x, y):
return str(x) * 8 + str(y) * 5
print (rep_cat(7, 2))
```

*Explanation of Challenge #4*

First, we convert the integers to strings using the `str( )`

method. We then can use the `*`

operator to replicate the strings the required number of times. To link the strings together, we used the `+`

operator, and finally, that new string is returned using the `return`

statement.

We use print to display the final statement.

Your challenge is to write the Fibonacci function so that is takes a positive number, `n`

, and returns the n-th number in the Fibonacci sequence using loops.

The Fibonacci sequence is a famous mathematical formula where each number in the sequence is the sum of the two numbers before it. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, etc.

Your input:

```
n = 10
```

Hint:

```
def fib(n):
first = ?
second = ?
if n < 1:
return ?
if n == 1:
return ?
if n == 2:
return ?
count = # where do we start the sequence?
while count <= n:
fib_n = ?
first = second
second = fib_n
count += 1
return ?
# what is our input?
# how do we print the answer?
```

Solution:

```
def fib(n):
first = 0
second = 1
if n < 1:
return -1
if n == 1:
return first
if n == 2:
return second
count = 3
while count <= n:
fib_n = first + second
first = second
second = fib_n
count += 1
return fib_n
n = 10
print (fib(n))
```

*Explanation of Challenge #5*

This challenge requires conditional statements. If `n`

is less than 1, it will return -1, and if `n`

is 1 or 2, it returns the first or second value. Note that the first two values are set, as they will always be fixed with this sequence.

We used a `while`

loop to complete the challenge, but a `for`

loop would also get the same result. We use the `count`

variable and start at 3 because we already know the first two values. The two previous terms, `second`

and `fib_n`

become first and second with every iteration of the code.

We then ask the program to print our specified value, `n`

.

Write a Python program that separates the highs and lows of a list of numbers (`num_list`

) then returns a list of the number of lows and highs, in that order. You will use the `count_low_high()`

function. These are your parameters.

- If a number is more than 50 or divisible by 3, it is considered high
- If these conditions are not met, the number is considered low
- If the list is empty, it will return none

Your input:

```
num_list = [77, 9, 95, 2, 51, 29, 12, 136]
```

Hint:

```
def count_low_high(num_list):
? = list(filter(lambda n : n > ? or n % ? == ?, ?))
? = list(filter(lambda n : n <= ? and not n % ? == ?, ?))
return (?)
num_list = [?]
print (?)
```

Solution:

```
def count_low_high(num_list):
high_list = list(filter(lambda n : n > 50 or n % 3 == 0, num_list))
low_list = list(filter(lambda n : n <= 50 and not n % 3 == 0, num_list))
return [len(low_list), len(high_list)]
num_list = [77, 9, 95, 2, 51, 29, 12, 136]
print (count_low_high(num_list))
```

*Explanation of Challenge #6*

To complete this challenge, you need to use the `filter()`

function, which filters the high numbers into one list and the low numbers into another. We set the parameters for sorting. We can then use the `len()`

function, which counts the elements in both lists. It isn’t necessary to use the `lambdas`

, but it simplifies the code. The lambdas keyword makes a shortcut to declare functions.

We then input our `num_list`

and ask the program to print our new list.

Well done! You completed all six Python challenges! You’re now a more seasoned and practiced developer. Don’t be discouraged if you got stuck on some of the challenges. The best way to learn is to identify places for improvement and study.

Remember that everyone learns at their own pace and style, so try not to compare your progress to others.

Happy learning!

#python

21.20 GEEK