0% found this document useful (0 votes)
6 views8 pages

Python 3 Basic Exercises Solutions

The document provides a series of Python exercises focused on basic programming concepts. It includes tasks such as calculating the area of a circle, finding maximum and minimum values without built-in functions, and determining the number of regions formed by straight lines. Each exercise includes a sample program and expected output to guide the user.

Uploaded by

khkoo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Python 3 Basic Exercises Solutions

The document provides a series of Python exercises focused on basic programming concepts. It includes tasks such as calculating the area of a circle, finding maximum and minimum values without built-in functions, and determining the number of regions formed by straight lines. Each exercise includes a sample program and expected output to guide the user.

Uploaded by

khkoo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python 3 – Extra Exercise 1 (Basics)

Reference:
[Link]
[Link]

1. Write a Python program which accepts the radius of a circle from the user and

compute the area.

Program
from math import pi
#to be completed

Sample Output
Input the radius of the circle: 1.1
The area of the circle with radius 1.1 is: 3.8013271108436504
2. Write a Python program to display the first and last colors from the following list.

Program
color_list = ["Red","Green","White" ,"Black"]

Sample Output
Red Black
3. Write a Python function to find the maximum and minimum numbers from a
sequence of numbers. Go to the editor
* Note: Do not use built-in functions.

Program
def max_min(data):
#to be completed
print(max_min([0, 10, 15, 40, -5, 42, 17, 28, 75]))

Sample Output
(75, -5)
4. Write a Python program to cut out words of 3 to 6 characters length from a given
sentence not more than 1024 characters.

Program
print("Input a sentence (1024 characters. max.)")
#to be completed

Sample Input
English sentences consisting of delimiters and alphanumeric characters are given
on one line

Sample Output
Input a sentence (1024 characters. max.)
This is a quick checking of the program
3 to 6 characters’ length of words:
This is a quick of the
5. if you draw a straight line on a plane, the plane is divided into two regions. For
example, if you pull two straight lines in parallel, you get three areas, and if you
draw vertically one to the other you get 4 areas.

Write a Python program to create maximum number of regions obtained by


drawing n given straight lines.

Program
while True:
print("Input number of straight lines (o to exit): ")
#to be completed
print("Number of regions:")
print((n*n+n+2)//2)

Sample Input
5

Sample Output
Input number of straight lines (o to exit):
5
Number of regions:
16
6. Write a Python function that takes a sequence of numbers and determines if all
the numbers are different from each other.

Program
def test_distinct(data):
#to be completed
print(test_distinct([1,5,7,9]))
print(test_distinct([2,4,5,5,7,9]))

Hint
set() creates a set object. The items in a set list are unordered, so it will appear in
random order and no duplicates, and it returns:
> an empty set if no parameters are passed
7. Write a Python program to find the number of notes (Sample of notes: 10, 20, 50,
100, 200 and 500 ) against an given amount.

Range - Number of notes(n) : n (1 ≤ n ≤ 1000000).

Program
def no_notes(a):
#to be completed
print(no_notes(880))
print(no_notes(1000))

Sample Output
6
2
8. Write a Python program to compute the digit number of sum of two given
integers.

Each test case consists of two non-negative integers x and y which are separated
by a space in a line.
0 ≤ x, y ≤ 1,000,000

Program
print("Input two integers(a b): ")
#to be completed

Sample Input
57

Sample Output
Input two integers(a b):
57
Number of digit of a and b.:
2

Common questions

Powered by AI

A Python program can ensure words of specific lengths are selected by iterating through a sentence, splitting the words, and applying conditional checks on their length. This approach is significant because it demonstrates control over string data, enables processing based on dynamic criteria, and can be adapted to a wide range of text analysis and processing tasks.

The mathematical constant pi is imported in a Python program to accurately calculate the area of a circle, as the area formula is A = πr², where r is the radius. The 'math' module in Python provides the constant 'pi', which ensures precision and avoids manually entering the value of pi. Using 'math.pi', the program can compute the area accurately when a radius is inputted by the user.

A Python program calculates the number of notes for a given amount using a greedy algorithm, which minimizes the number by starting from the largest denomination and moving towards smaller ones. It iteratively divides the remaining amount by each denomination, adds the quotient to a count, and updates the remainder. This logic ensures an optimal solution by reducing the number of notes required.

The rationale for not using built-in functions like max() and min() is to encourage the understanding and implementation of algorithmic thinking to solve problems. By developing a custom function to find maximum and minimum numbers, programmers enhance their grasp of logic and iteration, which are crucial skills in computer science and software development.

Python exercises encourage skills development beyond syntax by embedding problem-solving and critical thinking challenges. These exercises often require logical reasoning, algorithm design, and application of mathematical concepts. Such challenges cultivate deeper understanding by demanding programmatic solutions that are both correct and efficient, fostering a skill set aligned with real-world programming scenarios.

Using sample outputs in programming exercises illustrates expected outcomes, aiding learners in verifying and debugging their solutions. This practice helps bridge the gap between abstract problem statements and tangible solutions, guiding learners to achieve accurate results. It supports a clearer understanding of requirements and outcome expectations, enhancing iterative training and learning effectiveness.

The formula (n*n+n+2)//2 calculates the maximum number of regions formed by n straight lines on a plane. This formula is derived from combinatorial geometry, where each new line potentially intersects all previous lines, thus forming additional regions. Specifically, each line introduces new intersections and divisions to existing regions maximally. Understanding this formula requires synthesizing concepts of combinations and geometric properties.

A Python program uses the 'set' data structure to test for distinct numbers because sets inherently do not allow duplicate entries. When a sequence of numbers is converted to a set, duplications are automatically removed. Thus, comparing the length of the set with the original sequence helps determine if all numbers were distinct. This property of sets facilitates efficient and concise implementation.

Calculating the digit count of the sum of two integers emphasizes understanding of numeric properties and the application of basic arithmetic operations. This approach underscores the importance of number magnitude analysis, separate from simple addition, and requires converting the sum to a string to count digits, thus blending numerical computation with string manipulation techniques.

Python handles user input effectively for variable data types using functions like input(), int(), and float() to capture and convert input data as needed. For example, input() gathers data as a string, but it can be cast to an integer or float for arithmetic operations. By leveraging these conversions, Python accommodates diverse data processing needs for developing interactive applications.

You might also like