0% found this document useful (0 votes)
33 views2 pages

Python Programming Basics for Beginners

Uploaded by

s-zeyad.mawjoud
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)
33 views2 pages

Python Programming Basics for Beginners

Uploaded by

s-zeyad.mawjoud
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

University of Science and Technology

Communications & Information Engineering Program


CSCI 101: Introduction to Computer Science
First Program

Write Python programs to:


1. Calculate and print the area of a circle. 𝐴𝑟𝑒𝑎 = 𝜋𝑟 2

2. Calculate and print the area of a triangle. Area = 0.5*base* height

3. Calculate and print the average of 3 numbers. Avg = (x+y+z)/3

4. Calculate and print the distance between two points (x1,y1) and (x2,y2)
𝐷𝑖𝑠𝑡 = √(𝑥2 − 𝑥1 )2 + (𝑦2 − 𝑦1 )2

5. Given number of weeks, calculate and print the number of minutes.

6. Calculate and print the number of days in N hours and the remaining hours.

7. Calculate and print the number of trays, N, needed to hold X glasses if each tray
contains Y glasses.

8. If you distribute N apples in groups of 7 apples each, how many groups will contain
exactly 7 apples and how many apples remain?

9. Display N grams in the form of number of kilograms and number of grams.

10. Given temperature values in Celsius units (Tc), calculate the corresponding values
of Kelvin units (Tk) according to the following relations.

TF = TR − 459.67
9
TF = TC + 32
5
9
TR = TK
5

Page 1
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
First Program

11. Write a program that reads two values, X and Y, from the user. The program then
swaps the values of X and Y. You should print the values of X and Y before and
after swap.
Example:
Enter x:10
Enter y:20
Before swap: x=10, y =20
After swap: x=20, y =10

12. Write a program that asks the user for three one-digit numbers and then uses them
as units, tens, and hundreds to evaluate one 3-digit number out of them
Example:
Please enter units: 5
Please enter tens: 2
Please enter hundreds: 9
The number is: 925

Page 2

Common questions

Powered by AI

Python can be used to solve basic computational problems by implementing formulae using straightforward calculations. For example, calculating the area of a circle involves using the formula A = πr², where 'r' is the radius. The area of a triangle can be computed using A = 0.5 * base * height. To average three numbers, you can sum them and divide by three, represented by the formula (x + y + z) / 3 .

To compute the distance between two points (x1, y1) and (x2, y2) using Python, you need to employ the Euclidean distance formula: Dist = √((x2 - x1)² + (y2 - y1)²). This requires using arithmetic operations and the square root function, typically available through the math library in Python .

A Python function for converting Celsius to Kelvin can be implemented using the formula Tk = Tc + 273.15. You would define a function that takes temperature in Celsius as input and returns the converted temperature in Kelvin by adding 273.15 to the Celsius value .

Spatial measurements like area and distance can be efficiently integrated into Python programs by defining functions that encapsulate the necessary mathematical formulas and using Python's math library for operations like powering and square root extraction. For instance, the area of a circle can be computed using the function circle_area(r: float) -> float: return π * (r**2), while distance can be calculated using dist(x1, y1, x2, y2) involving the Euclidean formula .

Understanding basic arithmetic operations is essential in programming as it allows developers to implement solutions for numeric-based problems efficiently. Mastery of these operations enables programmers to perform precise calculations for quantity transformations and unit conversions, such as converting grams to kilograms or dividing quantities into specified units. This knowledge also assists in optimizing code for performance, reducing errors from manual calculation, and ensuring robust and versatile function design .

Swapping two variables in Python can be done using a temporary variable or simultaneously in one line. The first method involves storing one variable in a temporary space, assigning the second variable to the first, and then assigning the temporary value to the second variable. Alternatively, Python allows swapping directly using tuple unpacking: X, Y = Y, X. A common mistake to avoid is not ensuring that a temporary variable is used when that's the method chosen, which can result in losing the original data .

Python can handle repetitive distribution problems by using loops or mathematical operations to automate the process of dividing items into groups. For distributing N apples into groups of 7, you can use integer division (//) to find the number of complete groups and modulus operation (%) to find the remainder. This approach ensures the process is scalable and efficient without manual counting .

Python's versatility and simplicity make it exceptionally useful for transforming values and performing calculations, especially with user interaction involved. Python's dynamic typing and extensive libraries ease the handling of various data types and mathematical operations. Its input functions and straightforward syntax allow seamless acquisition and processing of user inputs for personalized computations, enhancing interactivity and responsiveness in programs .

The incremental build approach involves developing a Python program step-by-step by starting with small sections of code to solve part of the problem and gradually adding complexity. For mathematical formulas, one would begin by implementing basic calculations, verifying correctness individually, and then integrating them to form more complex solutions like combining area computations with conditionals to perform different operations based on input .

When converting time units like weeks to minutes or hours to days in programming, consider the precision of your calculations, any potential overflow or underflow with large numbers due to accumulated conversion factors, and the correct use of integer division for consistency. For example, one must multiply the number of weeks by 7 (days per week) and 1440 (minutes per day) for conversion to minutes or take modulo to find remaining hours after computing full days from given hours .

You might also like