Python Internals for Developers: Practice Python 3.x Fundamentals, Including Data Structures, Asymptotic Analysis, and Data Types
()
About this ebook
This book explains the built-in and user-defined data structures in Python 3.x. The book begins by introducing Python, its fundamental data structures, and asymptotic notations. Once you master the fundamentals of Python, you'll be able to fully comprehend the built-in data structures. The book covers real-world applications to understand user-defined data structures and their actual implementation. Towards the end, it will help you investigate how to solve practical problems by first comprehending the issue at hand.
After reading this book, you will be able to identify data structures and utilize them to solve a specific problem. You will learn about various algorithm implementations in Python and use this knowledge to advance your Python skills.
Related to Python Internals for Developers
Related ebooks
Python In - Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems Rating: 0 out of 5 stars0 ratingsPython Data Structures and Algorithms Rating: 5 out of 5 stars5/5Tiny Python Projects: Learn coding and testing with puzzles and games Rating: 4 out of 5 stars4/5Mastering Objectoriented Python Rating: 5 out of 5 stars5/5Functional Python Programming Rating: 0 out of 5 stars0 ratingsAdvance Core Python Programming: Begin your Journey to Master the World of Python (English Edition) Rating: 4 out of 5 stars4/5Mastering Python Design Patterns Rating: 0 out of 5 stars0 ratingsPython Rating: 0 out of 5 stars0 ratingsPython for Developers: Learn to Develop Efficient Programs using Python Rating: 0 out of 5 stars0 ratingsPython Essentials Rating: 5 out of 5 stars5/5Programming Problems in Ruby Rating: 0 out of 5 stars0 ratingsData Structures with Python: Get familiar with the common Data Structures and Algorithms in Python (English Edition) Rating: 0 out of 5 stars0 ratingsMastering Python Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsNumPy Beginner's Guide Rating: 5 out of 5 stars5/5300+ Python Algorithms: Mastering the Art of Problem-Solving Rating: 5 out of 5 stars5/5Python Handbook For Beginners. A Hands-On Crash Course For Kids, Newbies and Everybody Else Rating: 0 out of 5 stars0 ratingsPython Data Analysis - Second Edition Rating: 0 out of 5 stars0 ratingsAlgorithm Challenges: The Dojo Collection Rating: 0 out of 5 stars0 ratingsModular Programming with Python Rating: 0 out of 5 stars0 ratingsDistributed Computing with Python Rating: 0 out of 5 stars0 ratingsPython Interview Questions: Ultimate Guide to Success Rating: 0 out of 5 stars0 ratingsTest-Driven Python Development Rating: 5 out of 5 stars5/5
Trending on #Booktok
Icebreaker: A Novel Rating: 4 out of 5 stars4/5It Ends with Us: A Novel Rating: 4 out of 5 stars4/5A Court of Mist and Fury Rating: 5 out of 5 stars5/5The Love Hypothesis Rating: 4 out of 5 stars4/5The Assassin and the Pirate Lord: A Throne of Glass Novella Rating: 4 out of 5 stars4/5The Secret History: A Read with Jenna Pick: A Novel Rating: 4 out of 5 stars4/5If We Were Villains: A Novel Rating: 4 out of 5 stars4/5Powerless Rating: 4 out of 5 stars4/5Pride and Prejudice Rating: 4 out of 5 stars4/5Normal People: A Novel Rating: 4 out of 5 stars4/5The Summer I Turned Pretty Rating: 4 out of 5 stars4/5A Little Life: A Novel Rating: 4 out of 5 stars4/5Funny Story Rating: 4 out of 5 stars4/5Happy Place Rating: 4 out of 5 stars4/5Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones Rating: 4 out of 5 stars4/5Fire & Blood: 300 Years Before A Game of Thrones Rating: 4 out of 5 stars4/5Once Upon a Broken Heart Rating: 4 out of 5 stars4/5Better Than the Movies Rating: 4 out of 5 stars4/5Seven Stones to Stand or Fall: A Collection of Outlander Fiction Rating: 4 out of 5 stars4/5Dune Rating: 4 out of 5 stars4/5Finnegans Wake Rating: 4 out of 5 stars4/5Beauty and the Beast Rating: 4 out of 5 stars4/5Crime and Punishment Rating: 4 out of 5 stars4/5The 48 Laws of Power Rating: 4 out of 5 stars4/5Divine Rivals: A Novel Rating: 4 out of 5 stars4/5The Little Prince: New Translation Version Rating: 5 out of 5 stars5/5Rich Dad Poor Dad Rating: 4 out of 5 stars4/5Milk and Honey: 10th Anniversary Collector's Edition Rating: 4 out of 5 stars4/5The Lord Of The Rings: One Volume Rating: 5 out of 5 stars5/5Beach Read Rating: 4 out of 5 stars4/5
Reviews for Python Internals for Developers
0 ratings0 reviews
Book preview
Python Internals for Developers - Sonam Chawla Bhatia
CHAPTER 1
Python: A Quick Recap
Introduction
Python is one of many programming languages which exist in the software world to help developers solve real-time problems. Programming language is a formal language like English, Hindi, Spanish, and so on. Just like English or any other language used by humans to communicate with each other, the programming language is used for communication between humans and computers.
Many programming languages exist for human-computer communication. Now you must be wondering why we need so many programming languages for the same task, that is, communication. The following are some of the reasons why so many languages exist:
Some languages are easier to read and maintain than others.
All languages have different performance for different applications.
Different languages are suitable for solving different problems. For example, HTML can be used for developing websites and C language is best for embedded software development.
Languages differ in support of libraries to support common functions.
Some languages have fewer vulnerabilities than others, thus making them more secure. For example, Java is more secure than C.
Thus, developers choose the programming language based on the problem to be solved, the infrastructure to be used, and the type of software to be developed.
Structure
In this chapter, we will discuss the following topics:
Understanding Python
Variables and expressions
Variables
Variable names
Operators
Arithmetic operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Assignment operators
Operator precedence
Expressions
Comments
Conditional statements
The if statement
The if…else statement
The if…elif…else statement
The nested conditional statements
Loops and iterations
The while loop
The for loop
The break statement
The continue statement
Functions
Pre-defined functions
Objectives
By the end of this chapter, you will be able to install Python in your systems. You will be able to understand Python syntax and write basic programs in Python. You will get basic knowledge of interactive and script mod in Python. You will get an idea of how to write and run Python programs in both modes and also be able to understand the concept of functions in Python and how they are implemented.
Understanding Python
Python is a high-level language like C++, Java, and so on. High-level languages are more understandable by humans than low-level languages. High-level languages hide register, memory details from the developer. The computer understands low-level languages such as machine/assembly language. High-level languages convert solutions written in high-level languages to low-level languages.
Figure 1.1: Compiler takes source code and converts it into machine language code (executable file)
Python is an interpreted language. Table 1.1 shows the difference between Interpreted languages and Compiled languages.
Table 1.1: Difference between Interpreted and Compiled languages.
The following figure shows how the interpreter takes source code and execute the output
Figure 1.2: Interpreter takes source code and executes directly
Python can be downloaded and installed by following the instructions available at https://www.python.org/downloads/. Python code can be written in interactive mode or script mode. To run the Python code in interactive mode, open command prompt and then type py. If Python is successfully installed in your system, the command prompt will display as shown in Example 1.1.
Example 1.1: Run Python programs
py
Figure 1.3: Python prompt
Type the following commands in the command prompt:
x=3
print(x)
Figure 1.4: Interactive Mode
The preceding commands are being run in an interactive mode and output 3
is printed. To exit the interactive mode the quit() or exit() functions can be used:
exit()
Figure 1.5: Exiting Interactive Mode
To run your Python program in script mode, open any text editor or IDE and type the instructions that you want to execute, and save the file with the.py extension. To run the preceding instructions using script mode, type the following lines in the text editor or IDE:
x = 3
print(x)
Save the file with any name and ensure that the extension is.py, say, for example, test.py. Change the directory to the folder on the terminal where the .py file has been saved. In our case, the file is saved on the desktop, to change the directory, run the following command:
cd desktop
Figure 1.6: Change Directory
Run the program using the following command:
pytest.py
Figure 1.7: Executing Script
The script executes and prints the output 3
in command prompt, same as interactive mode. Script mode has the following benefits over interactive mode:
Scripts can be reused. They can be run again as many times as needed without the need to type all the instructions.
In case any error is encountered in the script, only the error code needs to be modified. In the case of interactive mode, correct instructions need to be typed again.
Thus, it is better to use the script mode for developing programs.
Variables and expressions
This section explains what variables are and how they are declared and defined in Python. The following subsections give details about operators and expressions in Python.
Variables
Variables in programming languages are names given by the developers to store values while developing programs. Take a look at the following example:
x = 3
Here, x
is a variable name given to store the value 3
. The preceding instruction is an assignment statement. It stores the value 3
in computer memory and names that memory as variable x
as shown in figure 1.8.
Figure 1.8: Computer memory portion. Value 3
is saved in one portion of memory and named as variable x
Naming a computer memory helps you to extract the value from the computer memory as and when required. The following instruction extracts the value of x
stored in computer memory and prints the value on output:
print(x)
Example 1.2: Assigning values to different variables
pi = 3.14
greeting = hello
i = 0
isFound = True
Variable names
Python language has some pre-defined rules for defining the variable names. These rules are considered while writing the syntax of the Python programming language. The following rules have been defined for naming variables:
Variable names can contain both letters and numbers.
Variable name can’t contain any special character except underscore (_).
Variable name can either start from a letter or underscore.
Python is case-sensitive. Thus, "name,
Name and
NAME" are three different variable names. But it is better to avoid defining similar variable names in the same file for better program readability.
Some developers define variable names in camel case for better understanding. Example: bookName, studentName, and so onIt is advisable to keep meaningful variable names such as count or name instead of c or n.
Python has some reserved words like if, else, and so on. These reserved words can’t be used as variable names. Reserved words for Python are given in table 1.2:
Table 1.2: Python reserved words
Using reserved words as variables causes syntax error as shown in figure 1.9:
Example 1.3: Assigning values to reserved variables
elif = 1
nonlocal = 1
raise = 1
Figure 1.9: Reserved words usage as variable name causes a syntax error
Operators
Like any other programming language, Python also has operators which are used to perform various operations on different values and variables. Values and variables on which an operation is performed are known as operands. Python has the following type of operators:
Arithmetic operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Assignment operators
Arithmetic operators
Arithmetic operators perform mathematical operations on operands such as addition, subtraction, and so on. Arithmetic operators are binary operators. Binary operators operate on two operands. Table 1.3 describes the various Python arithmetic operators:
Table 1.3: Python arithmetic operators
Example 1.4 shows few arithmetic operators and their output.
Example 1.4: Arithmetic operators
2+3
//Output
5
3-2
//Output
1
2*3
//Output
6
5/2
//Output
2.5
23%5
//Output
3
2**4
//Output
16
5//2
//Output
2
The preceding example shows different outputs obtained while using arithmetic operators. As shown in the preceding example, the modulus operator (%) is the same as the mathematical remainder operator which outputs the remainder when 23 is divided by 5 (which is 3). The exponentiation operator (**) is the same as the mathematical power operator which outputs the value of 24 (which is 16). The floor division operator (//) divides the operand’s and the output’s floor value of division like in the preceding example, 5/2=2.5 but 5//2 outputs floor value of 2.5 which is 2.
The addition and multiplication operators in Python can be used for strings too. Python addition operator adds two strings and merges them. Python multiplication operator can take one string and one integer as input. Example 1.5 gives examples of addition and multiplication operators on a string.
Example 1.5: Addition and Multiplication operators on a String
hello +
world"
//Output
‘hello world’
abc
+def
//Output
‘abcdef’
a
*3
//Output
‘aaa’
hello
*2
//Output
‘hellohello’
Comparison operators
Comparison operators compare values of operands and outputs true or false based on comparison output. Comparison operators are also binary operators. Table 1.4 describes the various Python comparison operators:
Table 1.4: Python comparison operators
Example 1.6: Comparison operators
2==3
//Output
False
john
==John
//Output
False
john
!=John
//Output
True
2!=3
//Output
True
2<3
//Output
True
‘a’<’b’
//Output
True
‘a’>’b’
//Output
False
5>2
//Output
True
2<=2
//Output
True
5>=2
//Output
True
The preceding example shows different outputs obtained while using comparison operators. Comparison operators can also be used to compare strings like in the preceding example comparison of "john with
John".
Logical operators
Logical operators are used in making decisions for conditions in a program. Logical operators perform operations on True/False operands. The output of the logical operator is either true or false. Table 1.5 describes the various Python logical operators:
Table 1.5: Python logical operators
Note: Unary operators operate on a single operand. The not
operator requires only one operand as shown in Example 1.7.
Example 1.7: Logical operators
TrueandTrue
//Output
True
TrueandFalse
//Output
False
TrueorFalse
//Output
True
FalseorFalse
//Output
False
not(True)
//Output
False
not(False)
//Output
True
Identity operators
Identity operators are like comparison operators. They are used to compare the objects and gives output true if both objects are exactly the same and are stored in the same memory. Identity operators are binary operators. Table 1.6 describes various Python identity operators:
Table 1.6: Python identity operators
Example 1.8: Identity operators
name = John
test = name
nameis test
//Output
True
a = 20
b = 20
ais not b
//Output
False
Membership operators
Membership operators check if the left operand is a part of the right operand. Membership operators are binary operators. Table 1.7 describes the various Python membership operators:
Table 1.7: Python membership operators
Example 1.9: Membership operators
’e’ in ‘hello’
//Output
True
‘z’ in ‘hello’
//Output
False
‘z’ not in ‘hello’
//Output
True
‘e’ not in ‘hello’
//Output
False
Bitwise operators
Bitwise operators operate on each bit of number. Bitwise operator converts operands to a binary number, performs operations on each bit, converts the binary output to a decimal number, and outputs decimal number. Table 1.8 describes the various Python membership operators:
Table 1.8: Python bitwise operators
Example 1.10: Bitwise operators
3&6
//Output
2
The preceding example shows the output of the Bitwise AND operator. The binary conversion of 3 is 011 and that of 6 is 110. Bitwise AND operator will take each bit of both the binary numbers and perform the AND operation as follows:
3&6 => 011 & 110 => 010 => 2
The decimal conversion of 010 is 2.
3|6
//Output
7
The preceding example shows the output of the Bitwise OR operator. The Bitwise OR operator will take each bit of both the binary numbers and perform the OR operation as follows:
3|6 => 011 | 110 => 111 => 7
The decimal conversion of 111 is 7. The following example shows the output of the Bitwise XOR operator:
3^6
//Output
5
The Bitwise XOR operator will take each bit of both the binary numbers and perform the XOR operation as follows:
3^6 => 011 ^ 110 => 101 => 5
The decimal conversion of 101 is 5. The following example shows the output of the Bitwise NOT operator:
~3
//Output
-4
The Bitwise NOT operator will take each bit of the binary number and reverses the bits. NOT reverses the sign bit of binary number too. Thus, the positive number becomes negative and the negative number becomes positive. The digned binary number of 3 is 0011.
~3 => ~0011 => 1100 => -4
The decimal conversion of 1100 is -4.
Note: In the case of a signed integer, the leftmost bit is the sign bit. For a positive number, the leftmost bit is 0 and for a negative number, the leftmost bit is 1. For 3, the signed binary representation is 0011. The first 0 represents that it is a positive number. Similarly, for -4, a signed binary representation is 1100, the first 1 represents that it is a negative number.
3<<1
//Output
6
3<<2
//Output
12
The preceding examples show the output of the left shift operator. It works as follows:
3<<1 => 0011<<1 => 0110 => 6 (shifted all bits by 1 position to the left)
3<<2 => 0011<<2 => 1100 => 12 (shifted all bits by 2 positions to the left)
The decimal conversion of 0110 is 6 and that of 1100 is 12.
6>>1
//Output
3
6>>2
//Output
1
The preceding examples show the output of right shift operator. Right shift operator works as shown as follows:
6>>1 => 0110>>1 => 0011 => 3 (shifted all bits by 1 position to the right)
6>>2 => 0110>>2 => 0001 => 1 (shifted all bits by 2 positions to the right)
The decimal conversion of 0011 is 3 and that of 0001 is 1.
Assignment operators
Assignment operators are used to assigning values to a variable. Some assignment operators are compound operators which are a combination of simple assignment and arithmetic/bitwise operators. Table 1.9 describes the various Python assignment operators:
Table 1.9: Python assignment operators
Example 1.11: Assignment operators
x = 5
print(x)
//Output
5
x+=2
print(x)
//Output
7
x-=5
print(x)
//Output
2
x*=3
print(x)
//Output
6
x/=2
print(x)
//Output
3.0
x%=2
print(x)
//Output
1.0
x=2
x**=3
print(x)
//Output
8
x//=3
print(x)
//Output
2
x&=3
print(x)
//Output
2
x|=3
print(x)
//Output
3
x^=6
print(x)
//Output
5
x<<=2
print(x)
//Output
20
x>>=2
print(x)
//Output
5
The preceding examples show that the compound assignment operators perform the operation on the left and right operand and then assign the result to the left operand.
Operator precedence
Just like mathematical operations have precedence, Python language also has defined precedence for operators. Operator precedence means the order in which the operations will be performed if single instruction has more than one operator. Python operator precedence is shown in Table 1.10 from higher precedence to lower precedence. Operators in the same precedence are executed from left to right (except for exponentiation, which is executed from right to left).
Table 1.10: Python operator precedence
Example 1.12: Order precedence
6-3+2*4/2
//Output
7.0
((6-3)+2)*4/2
//Output
10.0
The preceding example shows the order precedence for arithmetic operators with and without parenthesis. The first example, 6-3+2*4/2, is executed as follows:
6-3+2*4/2 => 6-3+8/2 => 6-3+4.0 => 3+4.0 => 7.0
The second example, ((6-3) +2) *4/2, is executed as follows:
((6-3) +2) *4/2 => (3+2) *4/2 => 5*4/2 => 20/2 => 10.0
Expressions
An expression is a combination of values, variables, and operators. A value or variable alone can also be considered as an expression. All the instructions shown in all the examples are expressions. Some other examples of expressions are shown in example 1.13.
Example 1.13: Expressions
X
5
X=5
X+=2
2+3
False
X=True
Comments
Comments help in the documentation of the solution being developed. As the program gets bigger and complex, comments help developers to understand the code. Comments start with # in a Python program. The Python interpreter ignores any statement starting with #.
Example 1.14: Comments
x=10
if(x%5==0): #Checks if x is divisible by 5
print (x, is divisible by5
)
//Output
10 is