Python Hello World
Summary: in this tutorial, you’ll learn how to develop the first program in Python called “Hello, World!”.
If you can write “hello world” you can change the world.
Raghu Venkatesh
Creating a new Python project
First, create a new folder called helloworld .
Second, launch the VS code and open the helloworld folder.
Third, create a new app.py file and enter the following code and save the file:
print('Hello, World!')
Code language: Python (python)
The print() is a built-in function that displays a message on the screen. In this example, it’ll show the message ‘Hello, Word!’ .
What is a function
When you sum two numbers, that’s a function. And when you multiply two numbers, that’s also a function.
Each function takes your inputs, applies some rules, and returns a result.
In the above example, the print() is a function. It accepts a string and shows it on the screen.
Python has many built-in functions like the print() function to use them out of the box in your program.
In addition, Python allows you to define your functions, which you’ll learn how to do it later.
Executing the Python Hello World program
To execute the app.py file, you first launch the Command Prompt on Windows or Terminal on macOS or Linux.
Then, navigate to the helloworld folder.
After that, type the following command to execute the app.py file:
python app.py
Code language: Python (python)
If you use macOS or Linux, you use python3 command instead:
python3 app.py
Code language: CSS (css)
If everything is fine, you’ll see the following message on the screen:
Hello, World!
Code language: Python (python)
If you use VS Code, you can also launch the Terminal within the VS code by:
- Accessing the menu Terminal > New Terminal
- Or using the keyboard shortcut Ctrl+Shift+` .
Typically, the backtick key ( ` ) locates under the Esc key on the keyboard.
Python IDLE
Python IDLE is the Python Integration Development Environment (IDE) that comes with the Python distribution by default.
The Python IDLE is also known as an interactive interpreter. It has many features such as:
In short, the Python IDLE helps you experiment with Python quickly in a trial-and-error manner.
The following shows you step by step how to launch the Python IDLE and use it to execute the Python code:
First, launch the Python IDLE program:
A new Python Shell window will display as follows:
Now, you can enter the Python code after the cursor >>> and press Enter to execute it.
For example, you can type the code print(‘Hello, World!’) and press Enter , you’ll see the message Hello, World! immediately on the screen:
Summary
- Use the python app.py command from the Command Prompt on Windows or Terminal on macOS or Linux to execute the app.py file.
- Use the print() function to show a message on the screen.
- Use the Python IDLE to type Python code and execute it immediately.
Python Classes
A is a programming construct that allows you to group functions and other data. Classes are basically templates for creating objects.
Python is an object-oriented programming (OOP) language that uses classes to define its objects. Basically, everything in Python is an object — lists, tuples, dictionaries, functions, even a string is an object.
All Python objects are based on classes. A class is basically a template for objects. It contains the definition of the object.
You can create your own classes too. The idea behind classes is that they provide a way of grouping functions and other statements that are logically related. For example, you might have a Customer class for grouping functions and other data related to each customer. You might also have a Product class for product related data, and so forth.
Create a Class
Here’s a quick example to demonstrate the concept:
This creates a class that contains one function. We then create an object from that class (here we call it o ). So once the object has been created, we can work with it.
Create Multiple Objects
We can create many objects based on the same class. In this example we create one object called o1 and another called o2 :
Create Multiple Functions
You’d normally put more than one function in a class. You can also pass arguments to the functions as you would any other function.
Here’s an example of a class that groups arithmetic related functions:
5 -292 6 6.0 55 -3245 7623 1.25
The __init__() Method
Python has a special method called __init__() that you can add to your class. The __init__() method is invoked whenever an object is created from the class. Therefore you can make use of the method without having to explictly call it.
Here’s a simple example to demonstrate:
We can access the self.a variable using o.a (i.e. objectname.variable ). So if we had an object called o1 and another called o2 then we could use o1.a and o2.a respectively.
About self
The self bit specifies that the code is for any instance that’s created, and that it’s not simply a local variable. For example, self.a means that once we’ve created an object from this class, we can then access that variable using dot notation (as we did using o.a ). This is known as an , commonly shortened to just .
So if we just wanted a local variable to work within the function, we could’ve used a = «Hey» . But because we wanted to be able to access that variable from our object, we used self.a = «Hey» .
It’s important to note that self actually has no special meaning in Python. There’s no self keyword in Python. Using self is nothing more than a convention. You could use anything, it doesn’t actually have to be self . For example, you could use arg .
However, before you decide to use something other than self , be aware that by not following the convention your code may be less readable to other Python programmers. It could cause problems in cases where a class browser program expects this convention.
Create a Person Class
We’ll now create a Person class, for grouping «person» related functions.
This class uses the __init__() method for setting various person related attributes.
It also has an updateEmail() function that can be called when someone’s email address needs to be updated.
When we instantiated the class, we set the email address to the empty string ( «» ), then printed the details (which showed no email address).
We then called the updateEmail() function, passing in an email address. So when we printed the details again, we saw that the user now has an email address.
Inheritance
Inheritance is a key concept in object oriented programming. Classes can inherit from other classes. This basically means that you can create a class based on another class.
So we could create a Customer class and base it on the Person class. We can then add attributes to that only customers will need, such as how much they’ve spent:
Peter Griffin (customer 12) has spent 13000 in total.
Here we use a (slightly) stripped down version of our Person class (from the previous example), then we create a Customer class based on it. We know it’s inherited from the Person class because we’ve put Person inside the parentheses (like this class Customer(Person): .
The Customer class has a function called getDetails() that returns a string of text about how much the customer has spent. This function uses the string formatting tokens %s and %i to insert the customer’s details into the right place. The details are provided after the % symbol after the end of the string.
How to Check an Object’s Class
You can check an object’s class with the object.__class__ attribute. Generally, you’ll get the same result when using the type() function.
Here’s how to output the class of an object (using both syntaxes):
What that tells us is that the string is based on the str class.
You can do the same thing for any object. Here are some more examples: