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

Python Basics: Input and Output Examples

The document contains a series of Python code snippets that demonstrate various programming concepts including user input, arithmetic operations, conditional statements, and data structures. Each snippet illustrates a specific functionality such as calculating sums, checking age for voting eligibility, and manipulating strings and lists. The examples serve as practical applications of basic programming techniques in Python.

Uploaded by

rhymeskalu
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)
4 views2 pages

Python Basics: Input and Output Examples

The document contains a series of Python code snippets that demonstrate various programming concepts including user input, arithmetic operations, conditional statements, and data structures. Each snippet illustrates a specific functionality such as calculating sums, checking age for voting eligibility, and manipulating strings and lists. The examples serve as practical applications of basic programming techniques in Python.

Uploaded by

rhymeskalu
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

1.

Favorite color
color = input("What is your favorite color? ")
print("Your favorite color is " + color + ".")

2. Sum of two variables


x = 56
y = 29
total = x + y
print("Your total price is " + str(total) + ".")

3. Two inputs, convert to integers, add, print


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("The sum is:", a + b)

4. Square root with math module


import math

num = float(input("Enter a number: "))


print("The square root is:", [Link](num))

5. Split full name into first and last


full_name = input("Enter your full name: ")
first, last = full_name.split()
print("First name:", first)
print("Last name:", last)

6. List of fruits, print second


fruits = ["apple", "banana", "cherry"]
print("The second fruit is:", fruits[1])

7. Age check (vote or not)


age = int(input("Enter your age: "))
if age >= 18:
print("You can vote.")
else:
print("You cannot vote.")

8. Chained decisions for test score


score = int(input("Enter your test score: "))

if score < 50:


print("Fail")
elif score < 75:
print("Pass")
else:
print("Distinction")

9. Nested decisions (even/odd + extra checks)


num = int(input("Enter a number: "))

if num % 2 == 0: # even
print("Even")
if num > 10:
print("It is greater than 10")
else: # odd
print("Odd")
if num < 5:
print("It is less than 5")

10. Conditional expression (positive/negative/zero)


num = int(input("Enter a number: "))
result = "Positive" if num > 0 else "Negative" if num < 0 else "Zero"
print(result)

11. Tuple of cities, print second


cities = ("New York", "Paris", "Tokyo", "London")
print("The second city is:", cities[1])

12. List of colors, change second


colors = ["red", "blue", "green"]
colors[1] = "yellow"
print(colors)

13. Format π with f-string


import math
print(f"{[Link].4f}")

14. Input check and modify


text = input("Enter something: ")

print("Type:", type(text))
print("Length:", len(text))

new_char = input("Enter a character to replace the first one: ")


modified = new_char + text[1:]

print("Modified string:", modified)

Common questions

Powered by AI

The document handles decision-making by employing if-else structures to evaluate conditions based on numerical inputs. Examples include checking if a user’s age is 18 or older for voting eligibility , determining pass levels for test scores with multiple conditions (Fail, Pass, Distinction), and evaluating whether a number is even or odd along with additional conditions like being greater than 10 or less than 5 . Further, a conditional expression is used to classify a number as Positive, Negative, or Zero based on its value . These examples showcase how the document applies logical structures to streamline the decision-making process based on user input.

The document illustrates manipulation of collections by showcasing operations on lists and tuples. For instance, it displays how to print an element from a list of fruits by accessing its index . Changing an element in a list is also demonstrated by replacing a color in a list of colors . Additionally, accessing a tuple to print a specific city is shown, emphasizing how to work with immutable structures . These examples underline different ways to interact with and manipulate data structures in programming.

The document presents several methods for string manipulation and formatting. It includes splitting a full name input into first and last components, demonstrating the split operation . String formatting is showcased by replacing the first character of a string with a user-provided character . Additionally, mathematical constants like π are formatted using f-strings to specify decimal precision . These examples highlight how strings can be transformed, formatted, and customized based on different requirements.

Indexing plays a crucial role in the document by allowing access to specific elements within lists and tuples. The document highlights this by printing the second item from a list of fruits and a tuple of cities . Indexing enables targeted retrieval of data, which is essential for operations that require accessing or changing specific elements in a collection. This is significant in efficiently managing and manipulating datasets, allowing programmers to pinpoint and modify elements directly.

The document employs modular programming concepts by leveraging the math module to perform operations like calculating the square root of a number, which demonstrates the use of pre-defined mathematical functions in a modular way . By importing the math module, it shows how complex operations can be conducted efficiently using established libraries without manually implementing these functions, underlining the benefits of modular programming for reusability and reliability.

The document uses control structures like if-else statements and conditional expressions to manage logical flow based on various conditions. It handles decision-making for voting eligibility, test score outcomes, and number classifications such as even or odd, demonstrating the use of logical conditions to dictate program behavior depending on user input . Moreover, chained and nested decisions are employed for more complex logic evaluations, indicating sophisticated control over the program's flow based on combined or sequential conditions.

The document illustrates input transformation by converting user input strings to integers before performing arithmetic operations, ensuring type compatibility for calculations . For string operations, user inputs are directly manipulated, such as when replacing characters in a string . While explicit validation mechanisms are not detailed, the conversion step implicitly validates that only numerical data types are processed arithmetically, preventing type errors during execution.

The document demonstrates the use of input to capture user data by prompting the user for inputs in different contexts. For example, it collects a favorite color and incorporates it into a response string . It also gathers numerical inputs to perform arithmetic operations or logical checks, like adding two integers after converting strings to integers . Additional examples include collecting a person's full name to split into first and last names and using input for making decisions based on the user's age or test score inputs .

The document shows various methods to handle arithmetic operations with user data by first converting input strings to integers and then performing calculations like addition . For instance, the sum of two variables x and y is calculated after defining them with numerical values . Similarly, it describes the calculation of a sum from user inputs by converting them to integers before the addition operation . Furthermore, the square root of a number is computed using the math module after receiving a float input .

The document uses a conditional expression to classify a number as Positive, Negative, or Zero in a concise manner . This approach offers clarity and reduces boilerplate code compared to traditional if-else statements, improving readability and maintainability by condensing logic into a single line. This technique is beneficial when dealing with straightforward conditional logic, as it simplifies the structure and enhances the efficiency of the code.

You might also like