Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Higher-Order Functions
Python basic
The print() Function
Once we understand variables, it’s important to see how we can display the values they store. This is where the print() function comes in. The print() function is one of the most commonly used functions in Python, and it allows you to output information to the screen.
How print() works?
The print() function takes one or more values (like variables, numbers, or text) and displays them in the output.
Example:
How print() works?
The print() function takes one or more values (like variables, numbers, or text) and displays them in the output.
Example:
print("Hello, world!") # This will output: Hello, world!
But what if you want to display the value of a variable? You can pass the variable inside print() to show its contents.
age = 25 print(age) # This will output: 25
In this example, the value stored in the variable age is displayed on the screen.
Combining text and variables:
You can also combine text and variables to make the output more informative. This is done by using commas or formatted strings (f-strings).
1. Using commas:
You can separate different items in print() with commas. Python will automatically add spaces between them.
name = "Alice" age = 25 print("Name:", name, "Age:", age)
Output:
Name: Alice Age: 25
2. Using f-Strings (formatted strings):
Python also allows you to insert variables directly inside a string by using an f before the string. This makes the code easier to read and cleaner.
name = "Alice" age = 25 print(f"My name is {name} and I am {age} years old.")
Output:
My name is Alice and I am 25 years old.
More examples of using print():
Let’s look at a few more examples to understand how print() can be used with variables:
price = 100 tax_rate = 0.2 total = price + (price * tax_rate) # Displaying variables print("Price:", price) print("Tax Rate:", tax_rate) print("Total Price:", total) # Using f-string print(f"The total price after tax is: {total}")
Output:
Price: 100 Tax Rate: 0.2 Total Price: 120.0 The total price after tax is: 120.0
In this example:
- We use print() to show the values stored in price, tax_rate, and total.
- The f-string allows us to create a more readable output that combines text and variables.
Why use print()?
When writing programs, it's important to see what your code is doing. Using print() helps you debug by showing the current values of your variables.
By printing values, you can test and learn how Python handles different types of data.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What is the primary purpose of the print() function in Python?
ATo output information to the screen
BTo calculate a value
CTo store data in variables
DTo read user input
Check Answer
Which line of code will correctly print the value stored in the variable age?
Aprint("age")
Bprint(age)
Cage.print()
Doutput(age)
Check Answer
What is the output of the following code: name = "Alice"; age = 25; print("Name:", name, "Age:", age)?
A"Name: Alice Age: 25"
B"Name Alice Age 25"
C"Name: Alice Age: 25"
D"Alice 25"
Check Answer
Which line of code correctly uses an f-string to print a variable?
Aprint(f("age is {age}"))
Bprint(f"age is {age}")
Cprint("age is f{age}")
Dprint("age is" + age)
Check Answer
How can the print() function help in debugging your code?
ABy displaying the current values of variables
BBy stopping the program if there is an error
CBy modifying the values of variables
DBy checking for syntax errors
Check Answer