You learned that a variable is a unit of data with an identifier, which is held in your computer’s memory, it can be changed by putting a new value into it or modifying the value that is already there. In this article, I will be introducing some of the different types of variables that are available for you to use in your programs, and I’ll be showing you how to build them into the expressions and statements of Python that will allow you to turn your designs into working code. This is where you start to do some real programming. You will be creating two programs from scratch in this article: one to manipulate and format simple text string and a script that performs a mathematical calculation. All this is made possible by using variables.
        Using variables allows you to specify a calculation or method for getting a result without having to know what values those variables will refer to beforehand. Any information that is put into the system must be turned into a variable before you can do anything to it, and it will be the contents of a variable that finally gets sent back to the user that called the program.

Choosing Good Identifiers

        Identifiers are the names used to identify things in your code. Python will regard any word that has not been commented out, delimited by quotation marks, or escaped in some other way as an identifier of some kind.
        An identifier is just a name label, so it could refer to more or less anything including commands, so it helps to keep things readable and understandable if you choose sensible names. You need to help to avoid choosing names that are already being used in your current Python session to identify your new variables. Choosing the same name as something else can make the original item with that name inaccessible.
        This could be particularly if the name you choose is an essential part of the Python language, but luckily Python does not allow you to name variables after any essential parts of the language. therefore, the next section contains an overview of the most important word used in Python, so you can avoid this problem; this is territory that you will be exploring and learning to work with throughout this book.

Python Keywords

        The following words are the keywords, which form the basis of the Python language. You are not allowed to use these words to name your variables, because these are the core commands of Python. They must be spelled exactly as written here for the interpreter to recognize them as keywords. The words True, False, and Node have a special meaning, which will be covered later.
False, None, True, and, as assert, a break, class, continue, def, del
elif, else, except, finally, for, from, global, if, import, in, is
lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield

Following the Naming rules

        So, let’s talk about what you are allowed to call your variables. Variable names must begin with either a letter or an underscore. Although they can contain numbers, they must not start with one. If the interpreter encounters a bunch of characters starting with a numeral, rather than a letter or a quotation mark, it will assume that it is a number.
        You should not use anything other than letters, numbers, or underscores to identify your variables. Also, you should be aware that Python is generally case-sensitive, which means that lowercase and uppercase letters are treated as being different characters; therefore, true and True are interpreted as completely different entities, as are myvariable, MYVARIABLES, and MyVariable.
        It also a good idea to keep your own record of names you have already used. I recommend keeping a table of variables at the start of the program file, so you can easily find your list when you want to look something up.

Creating Variables and Assigning Values

        In many programming languages, there are two stages to creating a variable: The first is to create the container and stick an identifying label on it; this is called initialization. The second is to put a value into it; this is called an assignment. Initialization and assignment are performed with a single command in Python, using the = sign. So you would assign a value to a variable by typing the following:
Variable = value code-box
        A section of code that does something, such as an assignment, is known as a statement. The part of the code that can be evaluated to produce value is known as an expression. The right-hand side of an assignment can be an expression, like the assignment to total_price in the following list of simple assignment statements:
number = 0
roll_width = 1.4
price_per_metre = 5
filename = ‘data.txt’
trace = False
sentence = “this is a whole lot of nothing”
total_price = roll_width*price_per_metre code-box
        each statement should have its own line. If this looks like a shopping list to you or a list of materials or ingredients, then you’re on the right track, A recipe usually begins with a list of ingredients:
eggs = 2
butter = 0.5oz
salt = pinch
pepper = pinch code-box
        the recipe might specify a list of tools that you will need—knife, fork, bowl, frying pan—and then follow on with methode, often in numbered steps. The same happens in a Python program; you define your variables and then carry out the task on them.

0Comments

Previous Post Next Post

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel