You are viewing the article How to Start Programming in Python at Lassho.edu.vn you can quickly access the necessary information in the table of contents of the article below.
wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, 48 people, some of whom are anonymous, have edited and improved the article over time.
This article has been viewed 33,338 times.
Want to learn programming? The process of getting used to programming can be daunting and make you think that you need to go to school seriously to do it. With some languages, this is sometimes true. But there are also many programming languages that only take one to two days for you to grasp the basics of them. Python [1] X Research Source is one such language. In just a few minutes, you can run a basic Python program. Read step 1 below to learn how.
Steps
Install Python (for Windows OS)
- You should download the latest version available, at the time this article was written, it was version 3.4.
- Python is available in OS X and Linux. You don’t need to install any other Python related software anymore. However, you should probably install a word processor program.
- Most Linux distributions and versions of OS X still use Python 2.X. There are some minor differences between version 2 and version 3, most notably a change in the “print” structure. If you want to install a newer version of Python for OS X or Linux, you can download the file from the Python website.
- Type print("Hello World!") and press the ↵ Enter key. Text Hello World! will be displayed just below the Python command line.
Learn the basics
- Python is one of the easiest languages to learn and you can run a simple program in just a few minutes.
Use the interpreter like a calculator
Doing a few simple math functions will familiarize you with Python’s syntax and how numbers and strings are handled.
- If you haven’t combined Python with an existing command-line interpreter, you’ll have to navigate to the Python directory to run the interpreter.
>>> 3 + 7 10 >>> 100 - 10 * 3 70 >>> ( 100 - 10 * 3 ) / 2 # Division always returns standard floating point (decimal) 35.0 >>> ( 100 - 10 * 3 ) // 2 # Integer division (two slashes) removes any decimals from the result. 35 >>> 23 % 4 # This command calculates remainder of division 3 >>> 17.53 * 2.67 / 4.1 11.41587804878049
>>> 7 ** 2 # 7 squared 49 >>> 5 ** 7 # 5 power 7 78125
>>> a = 5 >>> b = 4 >>> a * b 20 >>> 20 * a // b 25 >>> b ** 2 16 >>> wide = 10 # Variable can be a string literal any order >>> height = 5 >>> width * height 50
Create your first program
print ( "Hello World!" )
- Unlike many other languages, you don’t need to use the ; to end the command. You also don’t need braces ( {} ) to lock a block of commands. Instead, just indenting is enough to show what is contained in the command block.
- Make sure you save the file in an easily accessible location because you will have to find it in the command line interpreter.
- In this example, the file is saved as “hello.py”.
- Depending on how Python was installed and what version it is, you may have to type python hello.py or python3 hello.py to run the program.
Build advanced programs
# Each number in the Fibonacci sequence is # sum of the two numbers before it a , b = 0 , 1 while b < 100 : print ( b , end = ' ' ) a , b = b , a + b
- The thread will run as long as (while) b is less than (<) 100.
- The result will be 1 1 2 3 5 8 13 21 34 55 89
- The end=' ' command displays the results on the same line instead of leaving the values in individual lines.
- In this program there are some points that play a key role in creating complex programs in Python that you need to keep in mind as follows:
- Highlight the line indent. The : indicates that the following lines will be indented and part of the block. In the example above, print(b) and a, b = b, a+b are parts of the while block. Backing in the right way is very important in ensuring the operation of the program.
- Multiple variables can be defined on the same line. In the above example, a and b are both defined in the first line.
- If you enter this program directly into the interpreter, you must add a blank line at the end of the program to let the interpreter know that the program has ended there.
def fib ( n ): a , b = 0 , 1 while a < n : print ( a , end = ' ' ) a , b = b , a + b print () # At the back of the program you can use the # Fibonacci function for a specified value of any fib ( 1000 )
- It will return 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
age = int ( input ( "Enter your name: " )) if age <= 12 : print ( "It's great being a kid!" ) elif age in range ( 13 , 20 ): print ( "You're a teenager!" !" ) else : print ( "It's time to be an adult" ) # If any of the above conditions are true # the corresponding message will be displayed. # If any of the conditions are not met, the message "else" (different) # will be displayed.
- This program also introduces a few very important structures that are invaluable for a variety of applications:
- input() – This command requires the user to input data from the keyboard. The user will see the message written in parentheses. In this example, input() is wrapped by the int() function – meaning that any input will be treated as an integer.
- range() – This function can be used in a variety of ways. In this program, it checks whether the input number is between 13 and 20. The upper and lower bounds of the interval are not considered in the calculation.
Meaning | Sign | Python notation |
---|---|---|
Less | < | < |
Bigger | > | > |
Less than or equal | ≤ | <= |
Greater than or equal to | ≥ | >= |
Equal | = | == |
Not equal to | ≠ | != |
- There are many good books written about Python programming, including “Python for Beginners”, “Python Cookbook” and “Python Programming: An Introduction to Computer Science” (Python Programming: An Introduction to Computer Science).
- There are many resources out there on the net, but many of them are still geared towards Python 2.X. You’ll probably have to slightly tweak every example they provide.
- Many local schools offer classes in Python. Python is often taught in introductory classes because it is one of the easiest languages to learn.
Advice
- Python is one of the simpler computer languages. However, in order to learn, you still have to give it a certain amount of effort. Having an understanding of basic algebra is also helpful because Python is heavily focused on math.
wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, 48 people, some of whom are anonymous, have edited and improved the article over time.
This article has been viewed 33,338 times.
Want to learn programming? The process of getting used to programming can be daunting and make you think that you need to go to school seriously to do it. With some languages, this is sometimes true. But there are also many programming languages that only take one to two days for you to grasp the basics of them. Python [1] X Research Source is one such language. In just a few minutes, you can run a basic Python program. Read step 1 below to learn how.
Thank you for reading this post How to Start Programming in Python at Lassho.edu.vn You can comment, see more related articles below and hope to help you with interesting information.
Related Search: