Welcome to the exciting world of Python programming! Every journey begins with a single step, and in Python, that step is often using the print() function. Think of print() as your way of talking to the computer. It's the primary tool you'll use to display information, test your code, and see the results of your commands.
The basic syntax for the print() function is simple. You type print, followed by a pair of parentheses (). Inside these parentheses, you place whatever you want the computer to display. This could be text, numbers, or even the results of calculations.
print()Let's try printing some text. Text in Python, and many other programming languages, is called a 'string'. Strings are enclosed in quotation marks. You can use either single quotes (') or double quotes (") to define a string. Both will work perfectly fine, but it's good practice to be consistent within your project.
print('Hello, Python!')When you run this line of code, Python will interpret the print() command and display the text 'Hello, Python!' exactly as it's written inside the quotes, on your screen or in your console.
print("This is also a string!")You can also use print() to display numbers. Numbers are not enclosed in quotes because they are treated as numerical values, not as sequences of characters. This allows Python to perform mathematical operations on them later on.
print(123)print(3.14159)The print() function is incredibly versatile. It can even print the results of simple arithmetic operations. Python will perform the calculation first, and then print() will display the resulting number.
print(5 + 3)print(10 * 2)You can also print multiple items by separating them with commas inside the print() function. Python will automatically add a space between each item it prints.
print('My age is', 25)print('The result of 7 * 6 is', 7 * 6)graph TD
A[Start Program] --> B{Call print() function};
B --> C[Display output on screen];
C --> D[End Program];
As you can see, the print() function is your first and most fundamental command in Python. It's your direct line to seeing what your code is doing. Practice using it with different kinds of information – text, numbers, and simple calculations – to get comfortable. In the next sections, we'll delve into the different types of data you can work with in Python, building upon this essential print() function.