Writing "Hello World" in C++ and python

Writing "Hello World" in C++ and python

Writing "Hello World" in C++ and python

Hey there, fellow coders! Welcome to the second session of our coding club. Today, we're going to learn how to write the "Hello World" program and perform basic input and output in two of the most popular programming languages: C++ and Python. Don't worry if you're new to coding or if you're not familiar with these languages yet. We'll guide you through the process step-by-step and make it fun!

Writing "Hello World" in C++

First, let's start with C++. Open up your favorite text editor (we recommend using Visual Studio Code or Sublime Text) and create a new file called hello.cpp. In this file, type the following code:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World!" << endl;
    return 0;
}

Great! Let's take a closer look at what we just did.

We started by including the iostream library, which allows us to perform input and output operations in C++. Next, we used the using namespace std; statement, which tells the compiler that we want to use the std namespace. This namespace contains many useful functions, including the cout function that we'll be using shortly.

In the main() function, we used the cout function to print the text "Hello World!" to the console. We then used the endl function to insert a newline character, which moves the cursor to the next line. Finally, we used the return statement to exit the function with a status code of 0.

Writing "Hello World" in Python

Now, let's move on to Python. Open up a new file in your text editor and save it as hello.py. In this file, type the following code:

print("Hello World!")

That's it! Let's break it down.

In Python, we use the print() function to display text on the console. We simply passed the string "Hello World!" as an argument to the function, and it printed the text to the console.

Performing Basic Input and Output in C++ and Python

Now that we've learned how to write the "Hello World" program, let's take it a step further and learn how to perform basic input and output in C++ and Python.

Basic Input and Output in C++

In C++, we can use the cin and cout functions to perform input and output operations. Here's an example program that takes in a user's name and prints out a personalized greeting:

#include <iostream>
using namespace std;

int main() {
    string name;
    cout << "What's your name? ";
    cin >> name;
    cout << "Nice to meet you, " << name << "!" << endl;
    return 0;
}

Here, we declared a string variable called name to store the user's input. We used the cout function to display the prompt "What's your name?" on the console. We then used the cin function to read in the user's input and store it in the name variable. Finally, we used the cout function again to display a personalized greeting to the user.

Basic Input and Output in Python

In Python, we can use the input() and print() functions to perform input and output operations. Here's an example program that takes in a user's name and prints out a personalized greeting:

name = input("What's your name? ")
print("Nice to meet you, " + name + "!")

Here, we used the input() function to read in the user's input and store it in the name variable. We passed the prompt "What's your name?" as an argument to the input() function, which displays the prompt on the console and waits for the user to enter their input. We then used the print() function to display a personalized greeting to the user.

Comparing Syntax between C++ and Python

Now that we've learned how to write the "Hello World" program and perform basic input and output in both C++ and Python, let's take a moment to compare the syntax between the two languages.

Differences in Syntax

One of the biggest differences between C++ and Python is the use of semicolons. In C++, we use semicolons at the end of each statement, whereas in Python, we don't use semicolons at all. Additionally, C++ requires us to declare the data type of each variable, whereas Python is dynamically typed and infers the data type based on the value assigned to the variable.

Another difference is the use of curly braces to define code blocks in C++, whereas Python uses indentation. In C++, we enclose code blocks in curly braces {}. In Python, we use whitespace indentation to indicate the start and end of code blocks.

Similarities in Syntax

Despite their differences, C++ and Python share some similarities in syntax. For example, both languages use if statements to perform conditional execution, and both use loops (such as for and while loops) to repeat code blocks. Additionally, both languages use the + operator to concatenate strings, and the = operator to assign values to variables.

Conclusion

Congratulations, you've learned how to write the "Hello World" program and perform basic input and output in two of the most popular programming languages: C++ and Python! We hope this was a fun and informative introduction to these languages. Keep practicing and exploring, and don't forget to have fun while coding!

Did you find this article valuable?

Support Mojtaba Maleki by becoming a sponsor. Any amount is appreciated!