DEV Community

Cover image for Python Programming for Beginners – Day 3
augustineowino357-design
augustineowino357-design

Posted on

Python Programming for Beginners – Day 3

Python Input and Output Operations

In the previous lesson, we learned about Variables and Data Types in Python. Today, we will learn how Python accepts data from users and displays information on the screen using Input and Output operations.

Input and Output operations are very important because they allow programs to interact with users.


What is Output in Python?

Output means displaying information to the user.

Python uses the "print()" function to display output on the screen.

Example

print("Welcome to Python Programming")
Enter fullscreen mode Exit fullscreen mode

Output

Welcome to Python Programming

Enter fullscreen mode Exit fullscreen mode

Printing Variables
The "print()" function can also display values stored in variables.

Example

name = "Augustine"
age = 21

print(name)
print(age)
Enter fullscreen mode Exit fullscreen mode

Output

Augustine
21
Enter fullscreen mode Exit fullscreen mode

Printing Multiple Values

Python allows printing multiple values in one line.

Example

name = "Augustine"
course = "Python"

print("Student:", name, "Course:", course)
Enter fullscreen mode Exit fullscreen mode

Output

Student: Augustine Course: 
Python
Enter fullscreen mode Exit fullscreen mode

What is Input in Python?

Input means receiving information from the user.

Python uses the "input()" function to collect user input.

Example

name = input("Enter your name: ")

print("Welcome", name)
Enter fullscreen mode Exit fullscreen mode

Sample Output

Enter your name: Augustine
Welcome Augustine
Enter fullscreen mode Exit fullscreen mode

Important Note About input()
The "input()" function stores data as a String by default.

Example

age = input("Enter your age: ")

print(type(age))
Enter fullscreen mode Exit fullscreen mode

Output

<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Even if the user enters a number, Python treats it as text unless it is converted.


Converting User Input

We can convert input data into other data types using:

  • "int()"
  • "float()"
  • "str()"

Example

age = int(input("Enter your age: "))

print(age)
print(type(age))
Enter fullscreen mode Exit fullscreen mode

Output

21
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Simple Addition Program

Example

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

sum = num1 + num2

print("Total =", sum)
Enter fullscreen mode Exit fullscreen mode

Sample Output

Enter first number: 10
Enter second number: 20
Total = 30
Enter fullscreen mode Exit fullscreen mode

Escape Characters in Python
Escape characters are special characters used inside strings.

Escape Character| Meaning
"\n"| New Line
"\t"| Tab Space
"\""| Double Quotes
"\"| Backslash

Example

print("Python\nProgramming")
Enter fullscreen mode Exit fullscreen mode

Output

Python
Programming
Enter fullscreen mode Exit fullscreen mode

Comments in Python

Comments are used to explain code and improve readability.

Single-Line Comment

# This is a comment
print("Python")

Enter fullscreen mode Exit fullscreen mode

Multi-Line Comment

"""
This is a
multi-line comment
"""
Enter fullscreen mode Exit fullscreen mode

Why Input and Output are Important

Input and Output operations help programs:

  • Interact with users
  • Accept real-time data
  • Display results
  • Create interactive applications
  • Perform calculations based on user input

Without Input and Output, programs would not communicate with users effectively.


Conclusion
Input and Output operations are essential in Python programming. The "input()" function allows users to provide information, while the "print()" function displays results on the screen.

Understanding these concepts is important because almost every Python application uses Input and Output operations.

Practice creating programs that accept user data and display meaningful results.

Python #PythonForBeginners #CodingJourney #LearnPython #Programming

Top comments (0)