Programming in Python

Author: Frank Hunt
Date Of Creation: 13 March 2021
Update Date: 1 July 2024
Anonim
Learn Python - Full Course for Beginners [Tutorial]
Video: Learn Python - Full Course for Beginners [Tutorial]

Content

Do you want to learn how to program? Getting started programming in a programming language can seem daunting, and you may think it is necessary to take classes to learn this. While that may be the case for certain programming languages, there are several languages ​​that you can master in a day or two. Python is one of these languages. You can have already created a working Python program within a few minutes. Read on to learn how.

To step

Part 1 of 5: Installing Python (Windows)

  1. Download Python for Windows. You can download the Python interpreter for Windows for free from the Python website. Make sure to download the correct version for your operating system.
    • Download the most recent version.
    • Python is already included with OS X and Linux. There is no need to install all Python related software, but you really do need a good programming language word processor.
    • Most Linux distributions and versions of OS X still use Python 2.X. There are a few minor differences between 2 & 3, the changes to the "print" statement being particularly noticeable. If you want to install a newer version of Python on OS X or Linux, download the necessary files from the Python website.
  2. Install the Python interpreter. The default settings will work for most users. You can make Python work from the command prompt by checking the last option in the list of available modules.
  3. Install a word processor. It is possible to write a Python program in Notepad or TextEdit, but it is a lot easier to read the code with a specialized text editor. There are a number of free editors to choose from, such as Notepad ++ (Windows), TextWrangler (Mac), or JEdit (Any system).
  4. Test your installation. Open the Command Prompt (Windows command prompt) or the Terminal (Mac / Linux) and type python. Python will be loaded and the version number will be displayed. You will now see the Python interpreter command prompt, as follows >.
    • Type print ("Hello, World!") and press ↵ Enter. You will now get text Hello, World! can be seen under the Python command prompt.

Part 2 of 5: Learning the basic concepts

  1. There is no need to compile a Python program. Python works with an interpreter, which means that you can run a program immediately as soon as you make changes to it. This makes the process of iteration, revision and error finding a lot faster than in many other programming languages.
    • Python is one of the easiest languages ​​to learn and you can run a simple program within minutes.
  2. Using the interpreter. You can test with the interpreter code without actually adding it to the program first. This is great for learning how assignments work, or writing a one-time program.
  3. The way Python handles objects and variables. Python is an object-oriented language, which means that everything is treated as an object. This means that you have to declare variables at the beginning of a program (you can do this at any time), and you will also have to indicate the type of variable (integer, string, etc.).

Part 3 of 5: Using the Python Interpreter as a calculator

Performing some basic arithmetic functions helps to familiarize yourself with Python syntax and the way numbers and strings are handled.


  1. Start the interpreter. Open the Command Prompt or Terminal. Type python and press ↵ Enter. This starts the Python interpreter and opens the Python command prompt (>).
    • If you don't have Python installed so that you can run it from the command prompt, you will first need to go to the Python directory to run the interpreter.
  2. A few simple arithmetic operations. You can easily use Python to perform some simple arithmetic operations. See the code below for some examples of these calculation functions. Pay attention: # indicates that you are commenting in Python code, and are not processed by the interpreter.

    > 3 + 7 10> 100 - 10 * 3 70> (100 - 10 * 3) / 2 # Dividing always returns a floating point (decimal) number 35.0> (100 - 10 * 3) // 2 # Floor division (two slashes) ignores decimals 35> 23% 4 # Calculates remainder of division 3> 17.53 * 2.67 / 4.1 11.41587804878049

  3. Calculating powers. Use the ** operator to indicate a power. Python can quickly calculate large numbers. See code below with examples.

    > 7 * * 2 # 7 squared 49> 5 * * 7 # 5 to the power of 7 78125

  4. Creating and manipulating variables. You can assign variables in Python for simple algebraic functions. This is an excellent introduction to assigning variables in Python programs. You assign variables with the = sign. See code below with examples.

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

  5. Close the interpreter. When you are done using the interpreter, you can exit it and return to the command prompt by pressing Ctrl+Z (Windows) or Ctrl+D. (Linux / Mac) then on ↵ Enter. You can also quit () type, then press ↵ Enter presses.

Part 4 of 5: A first program

  1. Open your word processor. You can quickly create a test program to familiarize yourself with the basics of creating and saving programs, then run them with the interpreter. This also allows you to test whether your interpreter is properly installed.
  2. Making a "print" statement. "Print" is one of the basic functions of Python, and is used for displaying information in the terminal while running a program. Note: "print" is one of the biggest differences between Python 2 and Python 3. In Python 2, you just had to type "print" followed by what you wanted to display. In Python 3, "print" has become a function, so you will now have to type "print ()", with what you want to display between the brackets.
  3. Add a statement. One of the most common ways to test a programming language is to read the text "Hello, World!" to show. Place this text inside the "print ()" statement, along with the quotation marks:

    print ("Hello, World!")

    • Unlike many other languages, it is not necessary to add a semicolon at the end of a line ; to place. It is also not necessary to use curly braces ({}) to be used to indicate blocks with code. Instead, you work with indentation to indicate blocks of code.
  4. Save the file. Click File in the main menu of your word processor and select Save As. In the drop-down menu below the name box, choose the type of Python file. If you are using Notepad (not recommended), select "All Files" and put ".py" at the end of the file name.
    • Make sure to save the file where you can easily access it, as you should be able to access it easily from the command line.
    • First save it as "hello.py".
  5. Run the program. Open the Command Prompt or Terminal and navigate to the save location of the file. When you get there, run the program by typing hello.py and press ↵ Enter. You should now get the text Hello, World! below the command line.
    • Depending on how you installed Python and what version you may need to install python hello.py or python3 hello.py typing to run the program.
  6. Test as often as possible. One of the great things about Python is that you can test new programs right away. Good practice is to leave the command line on at the same time you are working in your editor. When you save a program in the editor, you can run the program right from the command line, so you can quickly test changes.

Part 5 of 5: Designing more complex programs

  1. Experiment with a standard flow control statement. Flow control statements allow you to control what a program does based on certain conditions. These statements are at the heart of Python programming, and you can use them to create programs that do different things depending on input and conditions. It while statement is a good example of this, to begin with. In this example you can do it while statement to calculate the Fibonacci sequence up to 100:

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

    • The sequence continues until (while) b is less than () 100.
    • The output then becomes 1 1 2 3 5 8 13 21 34 55 89
    • It end = "" command will show output on the same line, instead of showing each value on a different line.
    • There are a number of things to note in this simple program that are critical to creating complex programs in Python:
      • Note the indentation. A : indicates that the following lines will be indented and form part of a block of code. Create in the example above print (b) and a, b = b, a + b part of it while block. Proper indentation is essential and unique to a Python program. It will not work properly if the indentation is incorrect.
      • Multiple variables can be defined on the same line. In the example above, both a as b defined on the first line.
      • If you enter this program directly into the interpreter, you will need to add an empty line at the end so that the interpreter knows that the program has ended.
  2. Design functions within programs. You can define functions that you can call later in the program. This is especially useful when you need to use multiple functions within the confines of a larger program. In the following example, you create the same function as previously written, for calling a Fibonacci sequence:

    def fib (n): a, b = 0, 1 while an: print (a, end = '') a, b = b, a + b print () # Later in the program you call the Fibonacci function # for a value that you indicate. fib (1000)

    • This gives 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
  3. Build a more complex flow control program. With Flow control statements you can indicate specific conditions that change how the program functions. This is especially important when dealing with user input. The following example uses if, elif (else if) and else to create a simple program to comment on someone's age.

    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 are a teenager! ") else: print (" Time to grow up ") # If any of these statements are true # then the corresponding text will be displayed. # If none of the statements are true, then the "else" # message is displayed.

    • This program introduces a few other important statements that are indispensable for a number of different applications:
      • input () - This will ask for input with the keyboard. The user will see the message in quotation marks. In this example input () surrounded by the int () function, which means that all input is treated as an integer (integer).
      • range () - This function can be used in a number of different ways. In this program, it checks if a range falls between 13 and 20. The end of the range is not included in the calculation.
  4. Learn the other conditional expressions. The previous example uses the symbol "less than or equal to" (=) to determine if the entered age matches the condition. You can use the same conditional expressions you use in math, but typing them is slightly different:
    Conditional Expressions.
    MeaningSymbolPython Symbol
    Less than
    Greater than>>
    Less than or equal=
    Greater than or equal>=
    Equals===
    Not equal!=
  5. Keep learning. This is just the beginning when it comes to learning Python. Although it is one of the easiest languages ​​to learn, there is a lot to learn if you want to dig deeper into the language. The best way is to continue making programs! Remember that you can quickly write a few drafts of programs in the interpreter, and testing your changes is as simple as running the program again from the command line.
    • There are several good books on Python programming, including "Python for Beginners", "Python Cookbook" and "Python Programming: An Introduction to Computer Science".
    • There are a number of online resources available, but they mainly focus on Python 2.X. You may then need to modify the examples to make them work in Python 3.
    • Many schools offer lessons on Python. Python is often taught in introductory programming classes because it is one of the easiest languages ​​to learn.

Tips

  • Python is one of the simpler computer languages, but still requires dedication to learn really well. It also helps to have some knowledge of algebra, as Python is very focused on mathematical models .