-->

Python Basic Programming For Beginner

The Python programming language has a wide range of syntactical constructions, standard library functions, and interactive development environment features. Fortunately, you can ignore most of that; you just need to learn enough to write some handy little programs.



You will, however, have to learn some basic programming concepts before you can do anything. Like a wizard-in-training, you might think these concepts seem arcane and tedious, but with some knowledge and practice, you’ll be able to command your computer like a magic wand to perform incredible feats.

This chapter has a few examples that encourage you to type into the interactive shell, which lets you execute Python instructions one at a time and shows you the results instantly. Using the interactive shell is great for learning what basic Python instructions do, so give it a try as you follow along.

You’ll remember the things you do much better than the things you only read.

Entering Expressions into the Interactive Shell

You run the interactive shell by launching IDLE, which you installed with Python in the introduction. On Windows, open the Start menu, select All Programs 4 Python 3.3, and then select IDLE (Python GUI). On OS X, select Applications 4 MacPython 3.3 4 IDLE. On Ubuntu, open a new Terminal window and enter idle3.

A window with the >>> prompt should appear; that’s the interactive shell. Enter 2 + 2 at the prompt to have Python do some simple math.

>>> 2 + 2
4

The IDLE window should now show some text like this:

  1. Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit
  2. (AMD64)] on win32
  3. Type "copyright", "credits" or "license()" for more information.
  4. >>> 2 + 2
  5. 4
  6. >>>

In Python, 2 + 2 is called an expression, which is the most basic kind of programming instruction in the language. Expressions consist of values (such as 2) and operators (such as +), and they can always evaluate (that is, reduce) down to a single value. That means you can use expressions anywhere in Python code that you could also use a value.

In the previous example, 2 + 2 is evaluated down to a single value, 4. A single value with no operators is also considered an expression, though it evaluates only to itself, as shown here:

>>> 2

2

Errors Are Okay!

Programs will crash if they contain code the computer can’t understand, which will cause Python to show an error message. An error message won’t break your computer, though, so don’t be afraid to make mistakes. A crash just means the program stopped running unexpectedly.
If you want to know more about an error message, you can search for the exact message text online to find out more about that specific error. You can also check out the resources at http://nostarch.com/automatestuff/ to see a list of common Python error messages and their meanings.

There are plenty of other operators you can use in Python expressions, too. For example, Table 1-1 lists all the math operators in Python.

Math Operators from Highest to Lowest Precedence
Math Operators from Highest to Lowest Precedence
The order of operations (also called precedence) of Python math operators is similar to that of mathematics. The ** operator is evaluated first; the *, /, //, and % operators are evaluated next, from left to right; and the + and - operators are evaluated last (also from left to right). You can use parentheses to override the usual precedence if you need to. Enter the following expressions into the interactive shell:

  1. >>> 2 + 3 * 6
  2. 20
  3. >>> (2 + 3) * 6
  4. 30
  5. >>> 48565878 * 578453
  6. 28093077826734
  7. >>> 2 ** 8
  8. 256
  9. >>> 23 / 7
  10. 3.2857142857142856
  11. >>> 23 // 7
  12. 3
  13. >>> 23 % 7
  14. 2
  15. >>> 2 + 2
  16. 4
  17. >>> (5 - 1) * ((7 + 1) / (3 - 1))
  18. 16.0

In each case, you as the programmer must enter the expression, but Python does the hard part of evaluating it down to a single value. Python will keep evaluating parts of the expression until it becomes a single value, as shown in Figure.

Evaluating an expression reduces it to a single value
Evaluating an expression reduces it to a single value
These rules for putting operators and values together to form expressions are a fundamental part of Python as a programming language, just like the grammar rules that help us communicate. Here’s an example:

This is a grammatically correct English sentence.
This grammatically is sentence not English correct a.
The second line is difficult to parse because it doesn’t follow the rules of English. Similarly, if you type in a bad Python instruction, Python won’t be able to understand it and will display a SyntaxError error message, as shown here:

  1. >>> 5 +
  2. File "<stdin>", line 1
  3. 5 +
  4. ^
  5. SyntaxError: invalid syntax
  6. >>> 42 + 5 + * 2
  7. File "<stdin>", line 1
  8. 42 + 5 + * 2
  9. ^
  10. SyntaxError: invalid syntax

You can always test to see whether an instruction works by typing it into the interactive shell. Don’t worry about breaking the computer: The worst thing that could happen is that Python responds with an error message.

Professional software developers get error messages while writing code all the time.

Read More:

The Integer, Floating-Point, and String Data Types

Storing Values in Variables


0 Response to "Python Basic Programming For Beginner "

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel