Introduction To Python Data Structures
Understanding the python programming
Skills important for the programmer:
- Programmer need to know the programming language (Python) – need to know the vocabulary and the grammar, need to be able to spell the words in this new language properly and know how to construct well-formed “sentences” as per Python Language Syntax.
- In programming space, whatever we write in python language is the “story” and the solution programmer offering is the “idea”.
- It much easier to learn a second programming language such as JavaScript or C++ after one language like Python is Learnt
Words and sentences
Python vocabulary is actually pretty small. This “vocabulary” is called the “reserved words”. These are words that have very special meaning to Python. When Python sees these words in a Python program, they have one and only one meaning to Python. The reserved words used in Python are:
and | Del | global | not | with |
as | elif | if | or | yield |
assert | Else | import | pass | |
break | Except | in | raise | |
class | Finally | is | return | |
continue | For | lambda | try | |
def | From | nonlocal | while |
Example of simple Python statement :
- print(“Hello welcome”)
Sentence starts with the function print followed by a string of text ofour choosing enclosed in single quotes. The strings in the print statements are enclosed in quotes. Single quotes and double quotes do the same thing
Conversing with Python – Python interpreter’s
>>>
The >>> prompt is the Python interpreter’s way of communicating with the user.
Interpreter and compiler
Python is a high-level language intended to be relatively straightforward to read and write and for computers to read and process. Machine language is very simple and frankly very tiresome to write because it is represented all in zeros and ones:
001010001110100100101010000001111
11100110000011101010010101101101
Machine language is more complex and far more intricate than Python. Very few programmers write machine language. There are translators available to allow programmers to write in high-level languages like Python or JavaScript and these translators convert the programs to machine language for actual execution by the CPU.
Python Language translators fall into two general categories: (1) interpreters and (2) compilers.
An interpreter reads the source code of the program as written by the programmer, parses the source code, and interprets the instructions on the fly. Python is an interpreter.
Three general types of errors in Python:
Syntax errors: A syntax error means that the user has violated the “grammar” rules of Python.
Logic errors : A logic error is possible when program has good syntax but there is a mistake in the order of the statements or perhaps a mistake in how the statements relate to one another.
Semantic errors: The program is perfectly correct but it does not do what the user wanted or intended for the program to do
Section 2:
Variables, expressions, and statements
- A value is one of the basic things a program works with, like a letter or a number. The values we have seen so far are 1, 2, and “hello, world!”
- These values belong to different types: 2 is an integer, and “hello, world!” Is a string, so called because it contains a “string” of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks.
- The print statement also works for integers. We use the python command to start the interpreter.
- Variables selected need to be meaningful.
- Variable names can be arbitrarily long and can contain both letters and numbers, but they cannot start with a number.
- Variables may start with uppercase letters, but variable names with a lowercase letter (you’ll see why later).
Python reserves 35 keywords:
and | del | from | None | True |
as | elif | global | nonlocal | try |
assert | else | if | not | while |
break | except | import | or | with |
class | False | in | pass | yield |
continue | finally | is | raise | async |
def | for | lambda | return | await |
2.1 Operators and operands
- Operators are special symbols that are used for arithmetic computations like addition, multiplication, subtraction etc.. The values the operator is applied to are called operands.
- The operators +, -, *, /, and ** perform addition, subtraction, multiplication, division, and exponentiation, as in the following examples.
2.2 Expressions
An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x has been assigned a value):
Worked out Examples :