Giter Club home page Giter Club logo

ex5's Introduction

Python Introduction

Conditional statements

In this exercise, we will once more focus on the usage of the conditional statements, including:

  • if-else statements,
  • input from the user,
  • for and while loops,
  • comparison operators,
  • assignment operators,
  • logical operators.

Tasks

➕ Task 1 - calculate sum

Your task is to write a Python program to calculate the sum of three integers given (prompted) by user.
If the values are equal then calculate triple value of their sum.
Print out an appropriate message to the user.

  • Some of your results could look like this:
First number: 3
Second number: 4
Third number: 5
The sum is:  12

First number: 5
Second number: 5
Third number: 5
The triple sum is:  45

➕ Task 2 - get the difference

Your task is to write a Python program to get the difference between a two given numbers (prompted by user).
If the first number is greater than second then calculate double difference between numbers.
Otherwise calculate the absolute difference between numbers.
Print out an appropriate message to the user.

  • Some of your results could look like this:
First number: -2
Second number: -3
The result of calculation is  2

First number: 6
Second number: 6
The result of calculation is  0

First number: 4
Second number: 7
The result of calculation is  3

➕ Task 3 - odd or even number

Your task is to write a Python program to find whether a given number (prompted from the user) is even or odd. Print out an appropriate message to the user.

  • Some of your results could look like this:
Number to check: 43
43 is an odd number!

Number to check: 32
32 is an even number!

➕ Task 4 - circle area

Your task is to write a Python program which accepts (prompts) the float radius of a circle from the user and compute its area.
Round the result to two decimals!
Print out an appropriate message to the user.

Hint: You can import pi number from math module for convenience!

  • Some of your results could look like this:
Input the radius of the circle : 3.45
The area of the circle with radius  3.45  is:  37.39

➕ Task 5 - guess a number

Your task is to write a Python program to guess a number between 1 to 9.

Note : User is prompted to enter a guess. If the user guesses wrong then the prompt appears again until the guess is correct, on successful guess, user will get a "Well guessed!" message, and the program will exit.

Hint: you don't know the random module yet, so set your number to guess hard-coded in your program.

  • Some of your results could look like this:
Guess a number between 1 and 10 until you get it right : 9
Guess a number between 1 and 10 until you get it right : 3
Guess a number between 1 and 10 until you get it right : 4
Guess a number between 1 and 10 until you get it right : 5
Guess a number between 1 and 10 until you get it right : 6
Guess a number between 1 and 10 until you get it right : 7
Well guessed!

➕ Task 6 - Celsius to Fahrenheit conversion

Your task is to write a Python program to convert temperatures to and from Celsius, Cahrenheit.

In the centigrade scale, which is also called the Celsius scale, water freezes at 0 degrees and boils at 100 degrees.
In the Fahrenheit scale, water freezes at 32 degrees and boils at 212 degrees.

Note : User should be prompted twice:

  • to enter a temperature,
  • to enter a shortcut for given scale (C for Celsius, F for Fahrenheit).

Formula : C/5 = F-32/9, where C = temperature in Celsius and F = temperature in Fahrenheit.

  • Some of your results could look like this:
Input the scale shortcut you'd like to convert (F - Fahrenheit, C - Celsius: C
Input the value of temperature you'd like to convert  : 60
The temperature in Fahrenheit is 140 degrees.

➕ Task 7 - pattern

Your task is to write a Python program to construct the following pattern. Upper part should be done in one line of code without using a loop.
Lower part can be done with any kind of loop or also with one line of code and without loops.

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*

Hint: to print the same string few times you can use simple multiplication:

print(3 * '+ ')

And it gives three + signs:

+ + +
  • Some of your results could look like this:
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*

➕ Task 8 - Fibonacci series

Your task is to write a Python program to get the Fibonacci series between 0 to 50.

Note: The Fibonacci Sequence is the series of numbers : 0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Every next number is found by adding up the two numbers before it.

fib

Hint: There are few ways to do this task. You can use for example while loop and variable swapping.

  • Some of your results could look like this:
1
1
2
3
5
8
13
21
34

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.