The ability to manipulate variables is one of most powerful aspects of a computer language. A variable is an identifier that makes reference to a value.
Assignment statements
An assignment statement declares a new variable and assigns a value to it:
Figure 3.1. Perfroming assignment operations.
The first assigns the (approximate) value of π to pi, while message has string value 'Hello there'.
On paper, start writing the name of the variable followed by an arrow pointing to its value.
Figure 3.2. Pointers point to the assigned values in device memory.
This type of diagram is known as a state diagram since it depicts the state of the each variable.
Variable names could be as lengthy as you would like them to be.
They could include both letters and numbers, but they should not begin with a number.
Although it is legal to use uppercase letters, it is common practice to use only lowercase letters for variable names.
The underscore character, , can appear in a name. If you give a variable an illegal name, you get a syntax error:
Figure 3.3. Demonstration of some invalid assignment operation, naming convention of variables is to be followed.
These errors arise due to :
'7variable' is illegal because it begins with a number.
'more@' is illegal because it contains an illegal character, @.
'class' is one of Python’s keywords.
Python 3 has these keywords:
False | class | finally | is | return | None | continue |
for | lambda | try | True | def | nonlocal | while |
and | del | global | not | with | assert | else |
import | as | elif | or | yield | pass | expect |
in | raise |
Keywords are usually represented in a different color in most development environments. If you attempt to use one as just a variable name, it will fail.
Expressions and statements
A value, variable, and operator expression is just a mixture of values, variables, and operators.
When you enter an expression at the prompt, the interpreter analyzes it, which implies it determines the expression's value. Therefore in instance, the value of n is 17 and the value of n + 25 is 42.
A statement is a piece of code that performs an action, such as creating a variable or displaying a value.
Figure 3.5. Assigning value 17 to variable 'n'.
The very first line is just an assignment statement which assigns a value to the variable n. The second line contains a print statement showing the value of n. Once you enter a statement, the interpreter executes it.
Script mode
Python in interactive environment, which implies you can interact with the interpreter directly.
The interactive mode is a good starting point.
Another option would be to save code in a script file and then run the interpreter in script mode to execute the script.
Python scripts are identified by names that extension in '.py'.
Figure 3.6. Saved .py files appear with these icons on desktop or saved location.
As a demonstration, open File>>New File or press Ctrl+N
Figure 3.7. Creating a new .py file in python.
Create a program to display 'Hi there' message using a loop, and save it with the name Test.py, you can choose any name but it must end with the extension '.py'.
Figure 3.8. Printing the statement "Hi there" 10 times.
Then run the program by pressing Ctrl+F5 or Run>> Run Module F5
Figure 3.9. Output of the above program.
You will see the above as an output message.
Order of operations
While assessing sophisticated expressions such as 5+2*4% 6-1 and 13 or 3, it is easy to be become confused about order wherein the activities will be conducted.
The order of evaluation is determined by the order of operations. Python adheres to mathematical conventions when it comes to arithmetical operators.
The form of norms can be remembered by the below tabulation:
Operator | Description |
:= | Assignment expression (Lowest precedence) |
lambda | Lambda expression |
if-else | Conditional expression |
or | Boolean OR |
and | Boolean AND |
not x | Boolean NOT |
<, <=, >, >=, | Comparison operators |
!=, == | Equality operators |
in, not in, is, is not, | Identity operators, membership operators |
| | Bitwise OR |
^ | Bitwise XOR |
& | Bitwise AND |
<<, >> | Left and right Shifts |
+, – | Addition and subtraction |
*, @, /, //, % | Multiplication, matrix multiplication, division, floor division, remainder |
+x, -x, ~x | Unary plus, Unary minus, bitwise NOT |
** | Exponentiation |
await x | Await expression |
x[index], x[index:index], x(arguments…), x.attribute | Subscription, slicing, call, attribute reference |
(expressions…), [expressions…],
{key: value…}, {expressions…} |
Binding or parenthesized expression, list display, dictionary display, set display |
() | Parentheses (Highest precedence) |
Table 3.10. Table of assignment operators.
String operations
You can perform string operations using * and + operators.
String concatenation is performed by the + operator, which joins strings by linking them end-to-end.
The * operator can also be used on strings to conduct repetition.
For instance, 'Spam'*3 becomes 'SpamSpamSpam'. Another of the values must be a string, and another must be an integer.
We anticipate 'Spam'*3 to be much like 'Spam'+'Spam'+'Spam' as similar to 4*3 is equivalent to 4+4+4, and it is.
Comments
As projects grow in size and complexity, they become more hard to read.
Figure 3.11. The proper use of commenting can greatly simplify code maintenance while also assisting in the faster detection of bugs.
As a result, it is a great idea to include note cards in your programs that clarify what the program does in human language.
These memos are referred to as comments, and they begin with the # symbol:
Everything from the '#'
towards the end of the line is ignored and therefore has no impact on
the program's execution.
Comments are most helpful when those who record
non-obvious code features.
It is fair to presume that the viewer will be capable of figuring out what the code does. It would be more important to clarify why.
Although appropriate variable names could indeed reduce the need for comments, long identities can make complex expressions hard to read, and there is a tradeoff.
Debugging
A program can contain three types of errors: syntax errors, runtime errors, and semantic errors.
Figure 3.12. Debugging is a multistep process involving identification,isolation of the problem, followed by correcting it or determining a workaround.
Syntax error here refers to a program's framework and the rules governing that structure
The second type of error is a Runtime error, so named because the error does not appear until the program has begun to run.
Semantic error: The third type of error is "semantic," which means "meaning-related." If your program contains a semantic error, this will run without generating error messages, but will not do the right thing.
Copyright © 2022-2023. Anoop Johny. All Rights Reserved.