Introduction: Python Programming - Basics

Picture of Python Programming - Basics

This is the second instructable in the Python Programming Series.

These two links are essential references to better understand the fundamentals:

Python Programming - Environment Setup

Digital Logic Gates (Part 1)

Step 1: Booleans, Logical and Comparison Operators

Picture of Booleans, Logical and Comparison Operators

Booleans are either True or False.

The three logical operators defined on Booleans are:

1. not

2. and

3. or

These logical operators are listed in their order of operation as well. Also, the first figure in this step shows the rules of boolean algebra. As seen in the second figure, I have used the Python interpreter to do some examples in order to understand the function of each logical operator.

Examples

>>> not True

False

>>> False and True

False

>>>True or False

True

>>> True and not False

True

It is also important to know that Python has a boolean interpretation for non-boolean values. For instance, any non zero value is interpreted as True and zero is interpreted as False.

Furthermore, the following comparison operators are supported in Python:

1. == (equal)

2. != (not equal)

3. > (strictly greater than)

4. < (strictly less than)

5. >= (greater than or equal)

6. <= (less than or equal)

Comparison is an important concept in mathematics and it will come in handy in many of our programs.

Let's try some examples:

1. Is 6 greater than 4?

>>> 6 > 4

True

2. Is 4 less than or equal to 1?

>> 4 <= 1

False

3. Is 100.1 less than 100?

>> 100.1 < 100

False

Exercises

A. Boolean algebra exercises:
1. True and False or True

2. not 0

3. not 6

4. 1 and 0

B. Comparison exercises:

1. Is 1 a negative number?

2. Is 0 equal 1?

3. Is 5 greater than 4?

Note: Please share your answers in the comments section so we can discuss them.

Step 2: Numbers and Arithmetic Operators

Picture of Numbers and Arithmetic Operators

Python supports integers, floats, and complex numbers.

1. Integers: 0, 5, -6

2. Floats: 4.0, 5.2, -3.5

3. Complex numbers: complex(2,3) which denotes 2 + 3j

The mathematical operators shown in the figure attached to this step are built into Python and they can be used instantly. A very well known Mnemonic to easily remember the order of mathematical operations is PEMDAS, which stands for Parentheses, Exponent, Multiplication, Division, Addition, and Subtraction. I used the interpreter to do some examples and further explore numbers and arithmetic operators as shown in the second figure.

The parentheses can be used to force certain operations to be performed first.

Example 1

>>> 1 + 4 * 2

9

Explanation: The multiplication is performed first and thus the overall expression is 1 + 8

Example 2

>>> 6 - 3/2

4.5

Explanation: Division is performed first and thus the overall expression is 6 - 1.5

Example 3

>>> 2 + 2**2

6

Explanation: The exponent, which is 2 to the power of two, is performed first and thus the overall expression is 2+ 4

Example 4

>>>-3 * 4**2

-48

Explanation: The exponent, which is 4 to the power of two, is performed first and the overall expression is -3 * 16


It is important to note that the result of dividing two integers in Python 3 does not return an integer.

For instance:

>>> 10/2

5.0

>>> 9/2

4.5

>>> 9//2

4


A very useful built-in function in Python is round() and it is used to manipulate numbers. It rounds a given number to a specified number of digits.

For example:

>>> round(4.123,2)

4.12

>>> round(6.352,1)

6.4

>>> round(5.452)

5

Another useful function in Python is type() which returns the object type.

For example:

>>> type(5)

<class 'int'>

>>> type (5.1)

<class 'float'>

Exercises:

A. 12%5

B. 2 * 12%5

C. (1+2) * 3

D. 13/2

E. 13//2

F. round (13.5)

G. round(13.42,1)

Note: Please share your answers in the comments section so we can discuss them.

Step 3: Variables and Assignments

Picture of Variables and Assignments

Variables are used to refer to information that can change over time. Variables have names, and these names are used to access information.

For example:

>>> score = 10

>>> print(score)

10

>> result = False

>>>print(result)

False

The "=" sign is an assignment operator. Also, in other languages, such as C/C++, variables must be declared before assigning values to them. However, in Python, a variable does not have to be declared before assigning a value to it. It is critical to know that the left side of an assignment statement must be a variable name.

For instance:

score = 10 is not the same as 10 = score

result = False is not the same as False = result

Rules about variables:

1. Variable names can contain letters, numbers, and underscore characters, but they must begin with a letter or an underscore.

2. Keywords are reserved words that have a special meaning in Python and they cannot be used as variable names.

3.Variables should be lowercase with words separated by underscores as necessary to improve readability. For example: homework_grade, house_price, my_score

4. Variables are case-sensitive. For example: homework_grade is not the same as homework_Grade.

5. Built-in function names should not be used as variable names. If we use function names as variable names, the function will no longer be accessible.

The list of Python keywords can be shown as follows:

>>> import keyword

>>> keyword.kwlist

['False', 'None', 'True', 'and', 'as', 'assert', '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']

We will get an error if we specify an illegal variable name as seen below:

>>> 4score = 10

SyntaxError: invalid syntax

>>> False = 4

SyntaxError: can't assign to keyword

>>> amount$ = 50

SyntaxError: invalid syntax

Step 4: Constants

Unlike a variable, a constant refers to an identifier whose value is not supposed to change. There is no mechanism in Python that recognizes constants or treats them differently than variables. However, the convention is to use uppercase letters for the names of constants in order to be easily recognized.

For example:

LENGTH= 100

MAX_WEIGHT = 5

Explicitly identifying constant in our programs is a good practice because it makes our code more readable and maintainable.

Step 5: Comparison or Assignment?

Picture of Comparison or Assignment?

Distinguishing the difference between the assignment operator (=) and the comparison operator ( = =).

Here is an example:

>>> weight = 10

weight is a variable that is assigned to the value 10. The "=" sign is an assignment operator

>>> weight == 1

False

"= =" is a comparison operator, and weight == 1 is equivalent to "Is weight equal to 1?"

Also, "weight == 1" is just a comparison expression, and thus it does not change the value of the variable.

Step 6: Conclusion

Thank you for checking out this instructable. Please feel free to share any feedback, comments, or questions in the comments section.

In the next instructable, I will cover:

1. Strings.

2. Conversion functions.

3. Input and output.

4. Comments and documentation.

Comments

About This Instructable

180views

8favorites

License:

Bio: The DIY Pioneer
More by TalalKhalil:Python Programming - Basics Python Programming - Environment SetupArduino: HC-SR04 Ultrasonic Sensor
Add instructable to: