Table of Contents
This guide covers learn the first python program with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
Before you continue reading, make sure you don't miss any Python articles here.
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 codeprint("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)
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
num1Here is the variable, you can store the value in the variable and in this casenum1Stores the value 6.
Line 3:num2 = 9
Same as above, 9 is stored innum2.
Line 4:sum = num1+num2
The sum of variablesnum1Andnum2Is stored in another variablesum.
Line 5:print(sum)
Theprint()Functionprint()The output on the screen. In this case it will print the result 15 on the screen, after you press F5.
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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.