How to Start Programming in Python: 13 Steps

Table of contents:

How to Start Programming in Python: 13 Steps
How to Start Programming in Python: 13 Steps
Anonim

Do you want to learn to program? Starting to learn to code can be a bit intimidating and you may think you need to take classes in order to learn. While that may be true for many languages, there are a variety of programming languages that only take a day or two to learn the basics. Python is one of those languages. You can create a basic Python program and run it in a couple of minutes. Keep reading this article to learn how.

Steps

Part 1 of 5: Install Python

Windows

3574533 1
3574533 1

Step 1. Visit the Python website

You can download everything you need to get started with Python from the website (python.org/downloads. The website should automatically detect that you are using Windows and present the links to the Windows installer.

3574533 2
3574533 2

Step 2. Choose which version you want to install

Currently, there are two versions of Python available: 3.x.x and 2.7.10. Python makes both available to download, but new users should choose version 3.x.x. Download 2.7.10 if you are working with legacy Python code or with programs and libraries that have not yet adopted 3.x.x.

This guide will assume that you are installing version 3.x.x

3574533 3
3574533 3

Step 3. Run the installer after downloading it

Clicking the button for the version you want will download the installer. Run this installer after the download finishes.

3574533 4
3574533 4

Step 4. Check the "Add Python 3.5 to PATH" box

This will allow you to run Python directly from the command prompt.

3574533 5
3574533 5

Step 5. Click "Install Now"

This will install Python with all its default settings, which should be fine for most users.

If you want to disable some features, change the installation directory or install the debugger, click "Custom installation" and then check or uncheck the boxes

Mac

3574533 10
3574533 10

Step 1. Decide if you want to install Python 3.x.x

All versions of OS X come with Python 2.7 already installed. If you don't need the latest version of Python, you don't need to install anything. If you want to access the latest versions, you will need to install 3.x.x.

If you only want to use the included version of Python, you can create scripts in a text editor and run them through the terminal

3574533 11
3574533 11

Step 2. Download the 3.x.x files from the Python website

Visit (python.org/downloads on your Mac. This should detect your operating system and show the installation files for Mac. If it doesn't, click the "Mac OS X" link.

3574533 12
3574533 12

Step 3. Double-click the downloaded PKG file to start the Python installation

Follow the steps to install Python. Most users can use the default settings.

3574533 13
3574533 13

Step 4. Run Python in the terminal

To verify that the installation was successful, run the terminal and type python3. This should start the Python 3.x.x interface and display the version.

Linux

3574533 17
3574533 17

Step 1. Check the version of Python that you have already installed

Almost all Linux distributions come with Python installed. You can see what version you have by opening the terminal and typing python.

3574533 18
3574533 18

Step 2. Install the latest version on Ubuntu

Open the terminal window and type sudo apt-get install python.

You can also install Python using the Ubuntu Add / Remove Applications application located in the Applications window

3574533 19
3574533 19

Step 3. Install the latest version on Red Hat and Fedora

Open the terminal window and type sudo yum install python.

3574533 20
3574533 20

Step 4. Install the latest version on Arch Linux

Log in as the root user. Write pacman -S python.

3574533 21
3574533 21

Step 5. Download the IDLE environment

If you want to use the Python development environment, you can get it using your distribution's software manager. Just search for "python idle" to find and install the package.

Other essentials

167107 3
167107 3

Step 1. Install a text editor

While you can create Python programs in Notepad or TextEdit, it will be easier for you to read and write the code using a specialized text editor. There are a wide variety of free lime editors you can choose from, such as Notepad ++ (Windows), TextWrangler (Mac), or JEdit (any system).

167107 4
167107 4

Step 2. Test the installation

Open the command prompt (Windows) or Terminal (Mac and Linux) and type "python." Python will load and the version number will appear on the screen. The Python interpreter prompt will appear, which appears as ">>>".

Type "print (" Hello, World! ")" And press "Enter." You will see the text "Hello, World!" below the Python command line

Part 2 of 5: Learn the Basics

167107 5
167107 5

Step 1. Keep in mind that Python does not need compilations

Python is an interpreted language, which means that you can run the program as soon as changes are made to the file. This makes iterating, reviewing, and troubleshooting much easier than in other languages.

Python is one of the easiest languages to learn and you can create and run a basic program in a couple of minutes

167107 6
167107 6

Step 2. Get into the interpreter

You can use the interpreter to test the code without having to add it to the program first. This is great if you are learning how a specific command works or if you are writing a throwaway program.

167107 7
167107 7

Step 3. Learn how Python handles objects and variables

Python is an object-oriented language, which means that anything in the program is treated as an object. This means that you don't have to declare variables at the beginning of the program (you can do it whenever you want) and you don't need to specify the type of variable (integer, sequences, etc.).

Part 3 of 5: Use the Python Interpreter as a Calculator

Performing some basic calculator functions will help you become familiar with Python syntax and the way numbers and sequences are handled.

167107 8
167107 8

Step 1. Start with the interpreter

Open the command prompt or terminal. Type "python" at the command prompt and press "Enter." This will load the Python interpreter and the Python command prompt (>>>) will appear.

If you didn't embed Python in your command prompt, you need to navigate to the Python directory in order to run the interpreter

167107 9
167107 9

Step 2. Do some basic arithmetic

You can use Python to do basic arithmetic with ease. Read the chart below for a couple of examples on how to use the calculator functions. Note: # designates comments in Python code and does not pass through the interpreter.

>>> 3 + 7 10 >>> 100 - 10 * 3 70 >>> (100 - 10 * 3) / 2 # The division will always return a floating point number (decimal) 35.0 >>> (100 - 10 * 3) // 2 # The floor division (two diagonals) will discard the decimal results 35 >>> 23% 4 # This calculates the remainder of the division 3 >>> 17.53 * 2.67 / 4.1 11.41587804878049

167107 10
167107 10

Step 3. Calculate powers

You can use the "**" operator to indicate powers. Python can quickly calculate large numbers. Read the chart below for a couple of examples.

>>> 7 ** 2 # 7 squared 49 >>> 5 ** 7 # 5 to the seventh power 78 125

167107 11
167107 11

Step 4. Create and manipulate the variables

You can assign variables in Python to perform algebra. This is a good instruction on how to assign variables in Python programs. Variables are assigned using the "=" sign. Read the following chart for more examples.

>>> a = 5 >>> b = 4 >>> a * b 20 >>> 20 * a // b 25 >>> b ** 2 16 >>> width = 10 # The variables can be any sequence >>> height = 5 >>> width * height 50

167107 12
167107 12

Step 5. Close the interpreter

Once you are done using the interpreter, you can close it and return to the command prompt by pressing "^ Ctrl + Z" (on Windows) or "^ Ctrl + D" (on Linux and Mac) and then pressing "Enter." You can also type "quit ()" and press "Enter."

Part 4 of 5: Create Your First Program

167107 13
167107 13

Step 1. Open the text editor

You can quickly create a test program to familiarize yourself with the basics of creating, saving, and running programs through the interpreter. This will also help you check that the interpreter is installed correctly.

167107 14
167107 14

Step 2. Create the “Print” status

"Print" is one of the basic Python functions and is used to display information in the terminal during a program. Note: "print" is one of the biggest changes from Python 2 to Python 3. In Python 2, you just type "print" followed by whatever you want it to appear. In Python 3, "print" became a function, so you have to type "print ()", with whatever you want displayed between the parentheses.

167107 15
167107 15

Step 3. Add the state

One of the most common ways to test a programming language is for the text “Hello, World!” To appear. Put the text inside the "print ()" state, along with the quotes:

print ("Hello, World!")

Unlike many languages, you don't have to designate the end of the line with a ";". You also don't need to use square brackets "{}" to designate blocks. Instead, the indentation is what will indicate what is included in a block

167107 16
167107 16

Step 4. Save the file

Click on the "File" menu in your text editor and select "Save as." In the drop-down menu next to the name field, select the Python file type. If you use Notepad (which I don't recommend), select "All Files" and then add ".py" to the end of the file name.

  • Make sure to save the file in an easily accessible place, as you will have to navigate to it from the command prompt.
  • For this example, save the file as "hello.py".
167107 17
167107 17

Step 5. Run the program

Open the command prompt or terminal and navigate to the location of the file you just saved. Once there, run the file by typing "hello.py" and pressing "Enter." You should see the text "Hello, World!" below the command prompt.

Depending on how you installed Python, you may need to type "python hello.py" to run the program

167107 18
167107 18

Step 6. Test it often

One of the coolest things about Python is that you can test your program out of the box. A good practice is to have the program symbol open at the same time as your editor. When you save the changes in your editor, you can immediately run the program at the command prompt, allowing you to quickly test the changes.

Part 5 of 5: Create Advanced Programs

167107 19
167107 19

Step 1. Experiment with a basic flow control statement

Control flow statements allow you to control what the program does under specific conditions. These statements are the heart of Python programming, and they allow you to create programs that can do different things depending on the inputs and conditions. The "while" statement is a good start. In this example, you can use the "while" statement to calculate the Fibonacci sequence up to 100:

# Each number in the Fibonacci sequence is # the sum of the two previous numbers a, b = 0, 1 while b <100: print (b, end = '') a, b = b, a + b

  • The sequence will continue until (while) "b" is less than (<) 100.
  • The output will be "1 1 2 3 5 8 13 21 34 55 89"
  • The command “end = '‘”will display the output on the same line instead of putting each value on a separate line.
  • There are a couple of things you should notice about this program that are critical for creating complex Python programs:

    • Take note of the indentation. The ":" indicate that the following lines will be indented and are part of a block. In the above example, the command “print (b)” and “a, b = b, a + b” are part of the “while” block. Proper indentation is essential for your program to work.
    • Multiple variables can be defined on the same line. In the example above, "a" and "b" are defined by the first line.
    • If you are going to enter this program directly into the interpreter, you must add a blank line at the end so that the interpreter knows that it is finished.
167107 20
167107 20

Step 2. Create functions within the program

You can define functions that you can use later in the program. This is especially useful if you need to use multiple functions within the confines of a larger program. In the following example, you can create a function to create a Fibonacci sequence similar to the one you wrote earlier:

def fib (n): a, b = 0, 1 while a <n: print (a, end = '') a, b = b, a + b print () # Then you can use the # function again for any value you specify fib (1000)

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

167107 21
167107 21

Step 3. Create a more complicated flow control program

Control-flow statements allow you to set specific conditions to change how the program runs. This is very important, especially when you are dealing with user inputs. The following example will use the "if", "elif", and "else" functions to create a simple program that evaluates the age of the user.

age = int (input (“Enter your age:")) if age <= 12: print (“It's great to be a kid!") elif age in range (13, 20): print (“You're a teenager! ") else: print (" It's time to grow up ") # If any of the sentences is true # the corresponding message will appear. # If none of the statements is true, the "else" message # will appear.

  • This program also introduces a couple of very important statements that are very valuable in a wide variety of different applications:

    • ”Input ()” - This invokes user input from the keyboard. The user will see the message written in parentheses. In this example, the "input ()" is surrounded by an "int ()" function, which means that all input will be treated as an integer.
    • ”Range ()” - This function can be used in a variety of ways. In this program, you are checking to see if the number is in a range between 13 and 20. The end of the range is not counted in the calculation.
167107 22
167107 22

Step 4. Learn other conditional expressions

The example above uses the "less than or equal to" symbol (<=) to determine if the entered age meets the condition. You can use the same conditional expressions that you would use for math, you just have to type them differently:

'' Conditional expressions.

Meaning Symbol Python symbol
Smaller than < <
Greater than > >
Less than or equal to <=
Greater than or equal >=
It does not matter = ==
It is not the same !=

Step 5. Keep learning

These are basic examples when it comes to Python. Although it is one of the simplest languages to learn, it is also a very complex language if you are interested in doing more research. The best way to keep learning is by creating programs. Remember that you can create programs from scratch in the interpreter and test the changes by running the program from the command line.

  • There are many books available to learn about Python programming, including "Python for Beginners," "Python Cookbook," and "Python Programming: An Introduction to Computer Science." to computer science).
  • There are a wide variety of sources online, but most are focused on Python 2. X. You may need to make some adjustments to the examples that appear there.
  • If you want to run Python online, but want to run python 3, Repl [1] has a Python interpreter that uses virtual Linux machines. Another good online source for a future "pythonist" (very knowledgeable Python programmer) is thinkfunctional [2]. For bigger challenges, "Automate the Boring Stuff" [3] and Project Euler [4] are also available.
  • Many schools offer Python classes. The Python language is often taught as introductory classes and is one of the easiest languages to learn.

Advice

Popular by topic