0% found this document useful (0 votes)
22 views1 page

Data Science in Python Assignment 1

Uploaded by

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

Data Science in Python Assignment 1

Uploaded by

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

Assignment-1

1. Explain the different string formats available in Python with examples.


2. Write a program to repeatedly check for the largest number until the user
enters “done”.
3. Write a program to find the sum of all Odd and Even numbers up to a number
specified by the user.
4. Find the area and perimeter of a circle using functions. Prompt the user for input.
5. Illustrate the different types of control flow statements available in Python with
flowcharts.

Common questions

Powered by AI

The program can use a loop to repeatedly prompt the user for input, storing the largest number entered. Initialize a variable, e.g., 'largest', to None. Use a while loop that continues until the user inputs 'done'. Within the loop, convert inputs to numbers and compare to 'largest', updating it as necessary. If input cannot be converted to a number, handle as an exception. This logic ensures the program efficiently tracks the largest number without interrupting the flow until 'done' is entered .

Control flow statements manage the execution path through the program. Flowcharts visually represent these paths. Use diamonds for decision points (if statements), arrows for flow direction, and rectangles for execution blocks (loops, case statements). For example, illustrate an 'if-else' statement with a diamond leading to two paths: one for true and another for false conditions. Loops, such as 'for' and 'while', can be shown with arrows indicating the repetition until a condition changes. Such flowcharts convey logical structures more intuitively, aiding in design and debugging of complex logic .

The program can define separate sums for odd and even numbers, initialized to zero. Use a for loop to iterate over the range from 1 to the user-specified number. Inside the loop, use an if statement to check if each number is even or odd (using modulus operator), accumulating the sums in the corresponding variables. This efficiently computes the total for both odd and even numbers without requiring multiple iterations or additional data structures .

Python provides several string formatting options: (1) The '%' operator allows for basic old-style string formatting, similar to printf in C. For example, 'Hello, %s' % 'World' results in 'Hello, World'. (2) The 'str.format()' method, which provides more features, e.g., 'Hello, {}'.format('World'). (3) Formatted string literals, or f-strings, introduced in Python 3.6, enable in-line expressions, for example, f'Hello, {"World"}'. These formats allow integration of variables and expressions directly into strings, making them more readable and efficient .

Functions encapsulate logic and promote code reusability. For a circle, define two functions: one for area 'def area(radius)' returning 'pi * radius**2' and one for perimeter 'def perimeter(radius)' returning '2 * pi * radius'. These functions simplify calculations by separating concerns, improving code readability and maintenance. Prompt the user for the radius and call these functions, displaying results. Such modular design allows easy testing and future enhancements .

You might also like