Introduction to Python
Introduction to Python
Fundamentals of Python
Introduction to Python as a Computer Language
A product created from a computer language is called a computer application, or an application, or an app, or a computer program, or a program.
Python is a computer language. It can be used to create various types of applications. A console application is a program that displays its results in a blank (usually black) window. A Web application is a program that displays on one or more Web pages (a Web site). In this series of lessons, we will learn how to use the Python language to create console applications.
Acquiring and Using Python
To use Python, you will need to have it on your computer. If you are working in Microsoft Windows, to get Python, download it from the Microsoft Store (https://apps.microsoft.com/search?query=Python&hl=en-us&gl=US). Otherwise, you can download Python from its website: https://www.python.org/ where you will position your mouse on the Downloads link and select the option for your operating system. After downloading Python, you can install it (you can double-click the file that was downloaded and follow the instructions). When the installation is over, Python may automatically open:
At any time, you should check the installation of Python on your computer. To do that, if you are working in Microsoft Windows, open the Command Prompt, then type py -V and press Enter. The result will show you whether Python is installed on your computer and if so, what version. Here is an example:
A Python Project
Python can be used to create various types of applications. For our lessons, we will start with simple DOS applications that display their results on a wide black screen.
Using Microsoft Visual Studio
Introduction
You can use a programming environment or a programming studio or a studio to create an application. Many programming environments are available. To learn or use the Python language in Microsoft Windows, you can use Visual Studio Code. For our lessons, we will use Microsoft Visual Studio (Community) and sometimes Visual Studio Code. You can download any of them free from the Microsoft website.
Practical Learning: Starting a Project
A computer language consists of writing instructions, called code, that tell the computer what to do, how to do something, when to do it. To write these instructions, you can use a text editor, such as Notepad in Microsoft Windows. If you are using either Visual Studio Code or Microsoft Visual Studio, they provide their own area to write Python instructions. That area is referred to as the Code Editor.
Displaying Something on the Computer Screen
One of the operations you will perform for your applications is to display one or more values to the computer screen. To do this, type print(). In the parentheses of print(), you can type something.
A Version of Python
Like every computer application or language, Python works by a version. When you install Microsoft Visual Studio, it automatically installs a certain version of Python. Sometimes, that version is behind the current version. As a result, some versions of the language may not be supportted, in which case you may receive an error informing you that the feature you are trying to use is not available in the version of the Python you are using. As a result, you may need to update your version of Python some time to time.
To update your version of Python, as mentioned previously, if you are working in Microsoft, go to the Microsoft Store, do a searh on "python", locate the latest stable version, download, and install it to your computer.
Getting the Result of a Program
Introduction
You will write your Python code in plain English to give instructions to the computer, so that the computer can do some things on your behalf. You will write your instructions using words that a human being can read and understand. Unfortunately, the computer doesn't understand human languages, or not so easily. The computer understands a language known as machine language (made of series of 0s and 1s). To convert your English instructions to machine language, a computer language must use a special and appropriate application. That application depends on the type of language or the type of application.
Interpreting Code
One of the ways you can use the result of your instructions is to ask the computer to interpret your code. To make this happen, you can use a category of programs called an interpreter. An interpreter is a program that reads and analyzes your code line by line from top to bottom. As the interpreter proceeds line by line, every time it gets to a certain line, if that line has code that must produce a result (not all lines have (valid) code, and not all code produce any significant result), the interpreter gets that result. If the interpreter gets to a line that must present a result, the interpreter immediately presents that result. The interpreter continues like that, line after line.
While it is working line after line, if the interpreter gets to a line that has an error (or mistake), it may stop what it is doing (but this may depend on the interpreter, the type of language, the type of application, etc), then it lets you know so you can fix the error (sometimes, the interpreter tells you exactly on what line the error is; sometimes it points the error in a certain place while the actual error is on another line or on something else, but this is not the fault of the interpreter, many issues are involved here and we cannot go through all of them at this time). If there is no error, you will get the complete result that the program is supposed to produce (sometimes, a program doesn't produce an error but that doesn't mean that everything is correct the way it is supposed to be).
Compiling a Program
Another way you can see the result of your code is to compile it. In this case, you use a program named a compiler. Like an interpreter, a compiler reads and analyses your code line by line. Unlike the interpreter, a compiler doesn't immediately display the result of your code. The compiler continues with each line. If it finds an error, it may stop and let you know that there is a problem in your code. As long as the compiler doesn't find a problem, it continues going down the lines to the end. If everything seems alright in your code, the compiler (or computer) presents a result. The result may depend on the type of language and the type of application. For example, with some languages, the compiler creates a resulting application named an executable that can be installed in different computers (some languages, although compiled, are not made to produce an executable).
We mentioned that both the interpreter and the compiler are their own computer applications, and they have their own internal programs. For example, both have an internal program that is used to analyze code to check for errors. That internal program doesn't perform any true calculation; it only checks for syntax errors such a + b is correct but a !- b is not correct. If the syntax is valid, another program is asked to perform the necessary operations.
One way you can create programs in Python is to use an interpreter and/or compiler. When working in Python, you can either only interpret your code or you can compile it.
Building and Executing a Project
After writing your code, to see its results, you can (build and) execute the project, which is done by calling (invoking) the interpreter or compiler. The way you do this depends on the way you are using Python or it depends on the programming environment you are using. If you are working from the Command Prompt (Microsoft Windows), a Terminal (Linux, Apple), or Visual Studio Code, you would have to invoke the interpreter or the compiler. If you are working in Microsoft Visual Studio as we will do in our lessons, you can use the main menu or a shortcut.
Practical Learning: Writing Primary Code
When you work in a Python application, the Python compiler wants to know the version of the language you are using. If you are using the default version that Microsoft Visual Studio installs, you may now have to worry too much. As mentioned already, sometimes your version of Python may be outdated, and we have mentioned hoow to update it from the Microsoft Store.
To indicate to the compiler the version of Python you are using, you can create what is called an Environment file. This allows you to create a text file that will contain a line that indicates the version number. The compiler will refer to that file to know what version of Python it should use.
Practical Learning: Creating an Environment
Notice:After this section, whenever you start a new project in Microsoft Visual Studio (this will be different if you are using Visual Studio Code), you may have to create or add an environment. |
Notice:If you are working in Microsoft Visual Studio, if you install a new version of Python as seen earlier, sometimes when you execute (compiler) your project, you may receive a warning message box (this may no happen if you are using Visual Studio Code): If that happens, simply click OK and continue your work. |
Types of Values
Introduction
A computer deals with many types of values. The most fundamental values are those we have been using since birth (kindergarten, elementary school, etc).
A natural number is a value that has one or a combination of digits. Examples are: 1, 2608, 93, or 3485784. Normally, the number shouldn't have any character, but when the number is very long, you are allowed to separate the thousands with an underscore. In most computer languages, including Python, a natural number is called an integer.
Practical Learning: Creating Integers
print(1) print(8483) print(592) print(224_608) print(697_395_714)
1 8483 592 224608 697395714 Press any key to continue . . .
Floating-Point Numbers
A floating-point number is a number made in one or two parts. It can consist of the integral part that resembles the natural numbers we just saw above. A floating-point number can be in two parts: a natural part and a decimal part. The decimal part also appears like the natural part. Both parts are separated by a character named the decimal separator. In English, this character is the period. Examples of floating-point numbers are 2.48, 60.58, or 9287.485. If either part of the decimal separator is long, you can separate its thousands with underscores.
Practical Learning: Using Floating-Point Numbers
print(2.48) print(60.58) print(9287.485) print(283_794.73) print(8_428_074_242.824) print(62_594_508.405_437)
2.48 60.58 9287.485 283794.73 8428074242.824 62594508.405437 Press any key to continue . . .
Strings
A string is a character or a group of characters considered in one unit. To indicate a string, you can include it in quotes. You can use single-quotes or double-quotes. Just as done with the other values, you can write a string in the parentheses of print().
Practical Learning: Introducing Strings
print("y") print("n") print("Please submit your time sheet.") print("If your application is not working, click the Send button.") print("==========================================================")
y n Please submit your time sheet. If your application is not working, click the Send button. ========================================================== Press any key to continue . . .
print("I am not able to make up my mind on an area of study or my major. " "I am hesitating between: " "Petroleum Engineering, " "Computer Science," "and Construction Management.") print("At the end of your shift (which is sometimes at the end of a day woerk, " "remember to sign your time sheet and lock out of your computer.") print("==========================================================")
I am not able to make up my mind on an area of study or my major. I am hesitating between: Petroleum Engineering, Computer Science,and Construction Management. At the end of your shift (which is sometimes at the end of a day woerk, remember to sign your time sheet and lock out of your computer. ========================================================== Press any key to continue . . .
Introduction to Operations and their Operators
Introduction
An operation is a group of items that, when used together, must produce a result. An operator is a symbol that acts on one or two values. A value that participates in an operation is referred to as an operand. Consider an example of an operation such as x ? y \ z. In this case, x, y, and z are referred to as operators; ? and \ are referred to as operands.
The addition is used to add one value to another. The operation is performed with the + symbol. If it is used between two numbers, it produces the sum of them. If the operator is used on three numbers, the result is the sum of those three numbers. If this operator is used on many numbers, the result is the sum of all those numbers.
Practical Learning: Adding Values
print(8_428_074 + 242.824)
print(2.48 + 60.58)
print(9287.485 + 283_794.73)
print(62_594 + 508.405_437)
print("==========================================================")
8428316.824 63.059999999999995 293082.21499999997 63102.405437 ========================================================== Press any key to continue . . .
The addition operation can also be performed on strings. This operation is also referred to as concatenation. When one string A is added to another string B, in the result, the second string would come after the first string. Here is an example:
print("Sierra " + "Leone")
In the same way, you can add as many strings as you want. Here is an example:
print("This " + "is " + "very " + "impressive.")
The Subtraction -
Subtraction consists of removing one value from another. This operation is performed using the - operation. It can be performed on two or more values.
Practical Learning: Performing Subtractions
print(8_428_074 - 242.824) print(2.48 - 60.58) print(9795 - 735 - 49) print(9287.485 - 283_794.73) print(50862 - 594 - 405 - 437) print("=================================")
8427831.176 -58.1 9011 -274507.245 49426 ================================= Press any key to continue . . .
The Multiplication *
The multiplication consists of adding a value to itself a certain number of times. The operator used is: *. The formula to follow is: x * n. In this case, the value x is added to itself n times.
Practical Learning: Performing Multiplication
print(2.48 * 60.58) print(92.485 * 283_794.73) print("=================================")
150.23839999999998 26246755.60405 ================================= Press any key to continue . . .
print(8 * 428 * 74) print(62 * 9_416.88 * 38.405_437) print("=================================")
253376 22422882.27774672 ================================= Press any key to continue . . .
print("impressive " * 5)
print("======================================================")
impressive impressive impressive impressive impressive ====================================================== Press any key to continue . . .
The Division /
The division consists of getting a portion of a value. The operation is performed with /. The formula to use is x / y. x should be a number, any number. y should be a number and must never be 0.
Practical Learning: Performing Decimal Divisions
print(225 / 5)
print(4128 / 2)
print(93 / 462)
print(208404 / 314)
print(225.65 / 12.85)
print(974_259.84 / 106.06)
print("==================================")
45.0 2064.0 0.2012987012987013 663.7070063694267 17.560311284046694 9185.930982462756 ================================== Press any key to continue . . .
The Floor Division //
Usually, the division operation is used to get a decimal number made of a natural part and a fractional part. In some cases, we are not interested in the fractional part of the result. A division for which you are not interested in the fractional part of the result is referred to as a floor division. To perform that operation, use the // operator.
Practical Learning: Performing Floor Divisions
print(225 // 5) print(4128 // 2) print(93 // 462) print(208404 // 314) print(225.65 // 12.85) print(974_259.84 // 106.06) print("==================================")
45 2064 0 663 17.0 9185.0 ================================== Press any key to continue . . .
The Remainder %
When a division has been performed between two numbers, the remainder is the value that remains. This operation is performed with the % (percent) symbol.
Practical Learning: Getting the Remainder of a Division
print(225 % 5) print(4128 % 2) print(93 % 462) print(208404 % 314) print(225.65 % 12.85) print(974_259.84 % 106.06) print("==================================")
0 0 93 222 7.200000000000012 98.73999999994652 ================================== Press any key to continue . . .
The Power **
The power of a number consists of multiplying a number with itself a certain number of times. To perform this operation, use the ** operator.
Practical Learning: Getting the Power of a Number
print(225 ** 5) print(4128 ** 2) print(93 ** 6) print(208404 ** 4) print(225.65 ** 5) print(974_259.84 ** 3) print("==================================")
576650390625 17040384 646990183449 1886358360942251110656 585028049776.7169 9.247501332172279e+17 ================================== Press any key to continue . . .
Other Operators
The Pound # and Single-Line Comments
A comment is text that is written in a document but that text must not be considered as code. A comment can appear as valid Python code or regular text but it will not be processed as code.
To create a comment, type # and type the desired text of the comment. Anything on the right side of # until the end of a the line is considered a comment and will not be processed. Here is an example of a comment:
# The multiplication applied to a string
print("impressive " * 5)
print("======================================================")
The Semi-Colon ;
The semi-colon is used to indicate the end of a statement. You can use it at the end of a line that has print(). Here are examples:
print(8_428_074 - 242.824); print(2.48 - 60.58); print(9795 - 735 - 49); print(9287.485 - 283_794.73); print(50862 - 594 - 405 - 437);
Unlike some other languages, in Python, the semi-colon is not required to end a statement.
The Parentheses ()
The parentheses are used to create an isolated section that must be empty or must contain a list of items. The parentheses are ( and ). Although they are two items, parentheses are always used together, but not necessarily in one place. The left parenthesis, (, is used to start the section. The right parenthesis, ), is used to end the section. We will see that there are various ways to use parentheses. When it comes to numeric operations, you can use parentheses to isolate a particular operation in a group of operations.
The Double-Quote "
As seen previously, a string can be an empty space, a character, or a group of symbols. To indicate a string, you can use double-quotes and include the value in it. Here are examples:
The Multi-Line Comments
Earlier, we saw that, to create a comment, you could write # followed by text. That type of comment covers only one line. If you want to create a comment that covers more than one line, type six double-quotes """""". To create the text of the comment, type it after the first three double-quotes. Anything you write between the first three double-quotes and the other three double-quotes is considered a comment. This time, the comment can span many lines. Here is an example:
""" Python supports various types of operations, including:
The Addition +
The Subtraction -
The Multiplication *
The Decimal Division /
The Floor Division //
The Remainder %
The Power **
"""
print(8_428_074 + 242.824)
print(8_428_074 - 242.824)
print(2.48 * 60.58)
print(208404 / 314)
print(208404 // 314)
print(4128 % 2)
print(225 ** 5)
Single-Quotes
The single-quote is '. A string can be created with either the double-quotes or the single-quotes. Therefore, the above examples can also be written as follows:
In some cases, you want to create a string in a string. You have various options. The most common or simplest way is to start the main string with double-quotes. Include the internal string in single-quotes. Here is an example:
Escape Sequences
An escape sequence is a special character that displays non-visibly. For example, you can use this type of character to indicate the end of line, that is, to ask the program to continue on the next line. An escape sequence is represented by a backslash character, \, followed by another character or symbol. For example, the escape sequence that moves to the next line is \n. The escape sequences are:
Escape Sequence | Name | Description |
\a | Bell (alert) | Makes a sound from the computer |
\b | Backspace | Takes the cursor back |
\t | Horizontal Tab | Takes the cursor to the next tab stop |
\n | New line | Takes the cursor to the beginning of the next line |
\v | Vertical Tab | Performs a vertical tab |
\f | Form feed | |
\r | Carriage return | Causes a carriage return |
\" | Double Quote | Displays a quotation mark (") |
\' | Apostrophe | Displays an apostrophe (') |
\? | Question mark | Displays a question mark |
\\ | Backslash | Displays a backslash (\) |
\0 | Null | Displays a null character |
Curly Brackets
A curly bracket is a symbol used to delimit a section. There exist two curly brackets: the left or opening curly bracket { and the right or closing curly bracket }. There are various ways these brackets are used. Probably the primary two things to know are that these symbols are always used together but not in the exact same place. For example, the left bracket is usually used to start a section and the right bracket can be used to end that section. In some cases, the brackets are used by themselves, as in { }. In other cases, they must be combined with other symbols, such as {% %}.
Operation Precedence
Sometimes, you want to perform many operations in a group, in which case many operands and operators would be grouped. When this happens, some operations are performed before others. This characteristic is referred to as precedence. If you group many operations, you could get unpredictable results because of precedence. Consider the following example:
print(8_428 / 74 + 242.82 - 44) print(495 * 75 + 86 * 24 / 2.824) print("=================================")
This would produce:
312.71189189189187 37855.87818696884 ================================= Press any key to continue . . .
If you want to control which operation is performed before another, you can include that operation in parentheses. Consider the following examples:
print(8_428 / (74 + 242.82) - 44) print(495 * (75 + 86) * (24 / 2.824)) print("=================================")
This would produce:
-17.398144056562085 677294.6175637394 ================================= Press any key to continue . . .
The Assignment Operator =
The assignment consists of giving a value to something. This operation is done with the = operator. The formula to follow is: x = y. In this case, the value on the right side of =, in this case y, or the value of y, is given to the item on the left side of =, in this case x.
Practical Learning: Ending the Lesson
|
|||
Home | Copyright © 2021-2024, FunctionX | Wednesday 24 July 2024, 13:33 | Next |
|