Learn the first Python program

Before you continue reading, make sure you don't miss any Python articles here.

In this article we will learn a simple Python program to get a little more insight into Python, before starting to learn about the main components of this programming language.

Usually, when starting to learn any programming language from Pascal, C to HTML, the first program that almost all teachers guide us to write is "Hello, World!" or "Hello!" This simple program will print the screen with a greeting.

As mentioned in previous articles, Python is an easy-to-learn programming language, and the creation of "Hello, World!" The simpler it is, you just need to write the code print("Hello, World!") .

Therefore, we will go to learn another program, also simple, for you to understand a little more about this programming language. Temporarily name it Add two numbers:

 # Them hai so va in tong 
num1 = 6
num2 = 9
sum = num1+num2
print(sum)

Learn the first Python program Picture 1

What will the above code do?

Line 1: # Them hai so va in tong

Any line of code in Python starting with # will be a comment, a comment. It is used to describe what the code below is for. This will help other programmers, code readers, understand code better. Lines that begin with # will be ignored by code compilers and code interpreters.

Line 2: num1 = 6

num1 here is the variable, you can store the value in the variable and in this case num1 stores the value 6.

Line 3: num2 = 9

Same as above, 9 is stored in num2 .

Line 4: sum = num1+num2

The sum of variables num1 and num2 is stored in another variable sum .

Line 5: print(sum)

The print() function print() the output on the screen. In this case it will print the result 15 on the screen, after you press F5.

Learn the first Python program Picture 2

A few notes:

To represent a statement in Python, we use the new line (Enter). The use of the sign; at the end of the statement is optional (not required as in C / C ++, JavaScripts or PHP). In fact, you should ignore the signs; This is at the end of the Python statement.

Instead of brackets {}, indents are used to represent a block of statements. For example:

this_is_MS_Office:

this_is_Word:

this-is-Word-document

this_is_Excel:

this-is-Excel-spreadsheet

Let's wait and see in the next lesson, we will know more about Python.

Next lesson: Introducing about strings, numbers, and lists in Python

 

Previous article: How to install Python on Windows, macOS, Linux

4.5 ★ | 2 Vote

May be interested

  • How to set up Python to program on WSLHow to set up Python to program on WSL
    get started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
  • 5 choose the best Python IDE for you5 choose the best Python IDE for you
    in order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
  • Here are 4 reasons Python is not for youHere are 4 reasons Python is not for you
    should i learn python? is popularity meant that everyone should learn or not?
  • How to Start Programming in PythonHow to Start Programming in Python
    do you want to start learning how to program? getting into computer programming can be daunting, and you may think that you need to take classes in order to learn. while that may be true for some languages, there are a variety of...
  • How to install Python plugin to be able to execute Python programming on IntelliJHow to install Python plugin to be able to execute Python programming on IntelliJ
    is it possible to program python on intellij idea? instructions on setting up, installing python plugin to be able to program python on intellij ...
  • The function dir () in PythonThe function dir () in Python
    the dir () function in python returns a list of valid properties of the object. quantrimang will learn more about this function content through the article. invites you to read the track.
  • Built-in Python functionsBuilt-in Python functions
    in the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.
  • Manage files and folders in PythonManage files and folders in Python
    python also provides a variety of methods to handle various directory-related operations. in this article, we will learn about managing files and directories in python, namely creating folders, renaming folders, listing folders and working with them.
  • Print () function in PythonPrint () function in Python
    the print () function in python works to display the screen magic when the program executes.
  • What is Python? Why choose Python?What is Python?  Why choose Python?
    python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages ​​for people who are first exposed to programming languages.