How to Start Programming in Python

You want to learn programming? The process of getting used to programming can make you feel discouraged and 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.

Install Python (for Windows OS)

How to Start Programming in Python Picture 1

Download Python for Windows system. The Windows Python interpreter can be installed for free from the Python website. Please make sure that you have downloaded the correct version for your operating system.

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.

How to Start Programming in Python Picture 2

Install the Python interpreter. Most users can install the interpreter without changing any settings. You can merge Python into the built-in command line interpreter in Windows (Command Prompt) by enabling the last option in the list of available modules.

How to Start Programming in Python Picture 3

Install a word processor program. Whether you can write Python programs with Notepad or TextEdit, it's much easier to read and code using a specialized text editor. There are many free editing programs to choose from, such as Notepad++ (Windows), TextWrangler (Mac) or Jedit (for any operating system).

How to Start Programming in Python Picture 4

Check your settings. Open Command Prompt (Windows) or Terminal (Emulator - Mac/Linux) and type python. Python will load and display the version number. You will be redirected to the Python interpreter command line application, represented as: >>>.

Type print("Hello World!") and press the ↵ Enter key. Text Hello World! will be displayed just below the Python command line.

Learn the basics

How to Start Programming in Python Picture 5

Please understand that Python does not need to be compiled. Python is an interpreted language, which means you can run your program as soon as you make changes to the file. As a result, iteration, editing, and problem-solving programs run much faster than in other languages.

Python is one of the easiest languages ​​to learn and you can run a simple program in just a few minutes.

How to Start Programming in Python Picture 6

Explore with the interpreter. You can use the interpreter to test your code right away without having to add it to your program before you can test it. It's great for learning how a command works or writing a draft program.

How to Start Programming in Python Picture 7

Learn how Python handles objects and variables. Python is an object-oriented language, which means that everything in the program is treated as an object. Also, you don't need to specify the variable at the start of the program (which you can do at any time) and you don't need to specify the variable type (integer, string, etc.).

Use the interpreter like a calculator

Performing a few simple calculation functions will familiarize you with Python's syntax and how numbers and strings are handled.

How to Start Programming in Python Picture 8

Start the interpreter. Open your Command Prompt or Terminal. Type python at the prompt and press the ↵ Enter key. The Python interpreter will be loaded and redirect you to the Python command line interpreter application (>>>).

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.

How to Start Programming in Python Picture 9

Perform basic arithmetic operations. You can use Python to do it easily. Check out some examples in the box below on how to use the calculate function. Note: in Python code, # is used to refer to paragraphs that are followed by interpretation and, therefore, will not be included in 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

How to Start Programming in Python Picture 10

Calculate exponentiation. You can use the ** operator to represent exponentiation. Python can calculate large numbers quickly. Refer to the example in the box below.

>>> 7 ** 2 # 7 squared 49 >>> 5 ** 7 # 5 power 7 78125

How to Start Programming in Python Picture 11

Create and manipulate the same variables. You can assign variables in Python to perform simple algebraic calculations. This can be seen as a pretty good introduction to how to assign variables in a Python program. Variables are assigned via the = sign. For better understanding, see the example in the box below.

>>> 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

How to Start Programming in Python Picture 12

Close the interpreter. Once you're done, you can close the interpreter and return to the command-line interpreter application by pressing Ctrl+Z (Windows) or Ctrl+D (Linux/Mac) and then pressing ↵ Enter. You can also type quit() and press ↵ Enter.

Create your first program

How to Start Programming in Python Picture 13

Open a text editor. You can quickly create test programs to familiarize yourself with the basics of creating and saving programs and running them through the interpreter. It will also help you check if the interpreter is installed correctly.

How to Start Programming in Python Picture 14

Create a "print" command. "Print" is a basic function in Python, used to display information at the terminal in a program. Note: "print" is one of the biggest changes since moving from Python 2 to Python 3. In Python 2, you just need to type "print" before the content you want to display. In Python 3, "print" became a function. Therefore, you will have to type "print()", with the content you want to display enclosed in brackets.

How to Start Programming in Python Picture 15

Add your sentence. One of the most popular ways to test a programming language is to display "Hello World!". Put this sentence in the "print()" statement, including the quotes:

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. Instead, just indenting is enough to show what is contained in the command block.

How to Start Programming in Python Picture 16

Save file. Click the File menu in your editor and choose Save As. In the drop-down menu below the name box, select the Python file type. If using Notepad (not recommended), select 'All Files' and then add ".py" to the filename.

Make sure you save the file in an easily accessible location because you'll have to find it in the command line interpreter.

In this example, the file is saved as "hello.py".

How to Start Programming in Python Picture 17

Run the program. Open Command Prompt or Terminal and navigate to the location where you saved the file. Once there, run the file by typing hello.py and then pressing the ↵ Enter key. You will see the words Hello World! is displayed just below the prompt pointer.

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.

How to Start Programming in Python Picture 18

Test run regularly. One of the best things about Python is that you can try new programs right away. Opening the command line interpreter and editor at the same time is a good practice to do. When you save changes in the editor, you can run the program immediately from the command line. This allows you to quickly check the changes that have just been made.

Build advanced programs

How to Start Programming in Python Picture 19

Experiment with basic flow control structures. The flow control structure allows you to control what the program does based on certain conditions. These constructs are the essence of Python, allowing you to create programs that perform different operations based on input and given conditions. While is a good starting point to get familiar with them. In the example below, you can use the while structure to calculate the Fibonacci sequence up to 100:

# Each number in the Fibonacci sequence is # the 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 as part of a block of code. In the example above, print(b) and a, b = b, a+b are parts of the while block. Properly stepping back in 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.

How to Start Programming in Python Picture 20

Build a function in the program. You can define functions for later use in your program. This is especially useful in cases where you need to use many functions within the confines of a larger program. In the example below, you could create a function to call the same Fibonacci sequence as above:

def fib(n): a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print() # Later in the program you can use the Fibonacci # function for any specified value fib(1000)

It will return 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

How to Start Programming in Python Picture 21

Build a more complex flow control program. The flow control structure allows you to set specific conditions to change the way your program behaves. This is especially important when dealing with user input. The following example will use if (if), elif (else if) (or if), and else (else) to create a simple user age evaluation program.

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 mature") # If any of the above conditions are true # the corresponding message will be displayed. # If any 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.

How to Start Programming in Python Picture 22

Learn other conditional expressions. In the previous example, we used "less than or equal" (<=) to determine whether the entered age satisfies the condition. You can use the same expressions as in math, but type it a little differently:

Condition Expressions.
Meaning Sign Python notation  
Less < <  
Bigger > >  
Less than or equal <=  
Greater than or equal to >=  
Equal = ==  
Not equal to !=  

Continue learning. These are just the basics of Python. Despite being one of the simplest languages, if you want to dig deeper, Python is still very in-depth. The best way to continue learning is to keep building the program! Remember that you can quickly write any program straight into the interpreter and test the changes that have been made simply by running the program again from the command line.

There are many good books written on 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 Python classes. Python is often taught in introductory classes because it is one of the easiest languages ​​to learn.

5 ★ | 1 Vote | 👨 270 Views
« PREV POST
NEXT POST »