Basic Core Python Programming: A Complete Reference Book to Master Python with Practical Applications (English Edition)
By Meenu Kohli
()
About this ebook
Equipped with a lot of exercises and Q&As, you don’t just practice the programming but also gain an in-depth understanding of the basic concepts of Python. You will start your journey right from how to go about Python installation and start using its interactive development environment and go on to learn how to build logic and implement it with coding. You will explore different types of data, operators, and in-built functions. This book covers numerous coding examples that will help you understand the importance of each data type, how to work with each one of them, and when to use them. You can learn some more practical useful concepts like how to implement control structures and use them for decision making and controlling the program flow.
Read more from Meenu Kohli
Advance Core Python Programming: Begin your Journey to Master the World of Python (English Edition) Rating: 4 out of 5 stars4/5Python Interview Questions: Ultimate Guide to Success Rating: 0 out of 5 stars0 ratings
Related to Basic Core Python Programming
Related ebooks
Python In - Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems Rating: 0 out of 5 stars0 ratingsPython Made Simple: Learn Python programming in easy steps with examples Rating: 5 out of 5 stars5/5Python Programming Rating: 0 out of 5 stars0 ratingsPython for Developers: Learn to Develop Efficient Programs using Python Rating: 0 out of 5 stars0 ratingsThe Ultimate Python Programming Guide For Beginner To Intermediate Rating: 4 out of 5 stars4/5Data Structure in Python: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsProgramming Concepts in C++ Rating: 0 out of 5 stars0 ratingsEasy-To-Follow Tutorial To Learn Python Programming In Less Than One Week Rating: 3 out of 5 stars3/5Introduction to Programming Languages Rating: 4 out of 5 stars4/5The Art of Code: Exploring the World of Programming Languages Rating: 0 out of 5 stars0 ratingsPython Handbook For Beginners. A Hands-On Crash Course For Kids, Newbies and Everybody Else Rating: 0 out of 5 stars0 ratingsData Analysis with Python: Introducing NumPy, Pandas, Matplotlib, and Essential Elements of Python Programming (English Edition) Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Computer Programming Python, Machine Learning, JavaScript Swift, Golang: Rating: 0 out of 5 stars0 ratingsPython Programming: Your Beginner Guide To Learn Python in 7 Days Rating: 4 out of 5 stars4/5Computer Programming Languages for Beginners Rating: 0 out of 5 stars0 ratingsPYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Data Structures with Python: Get familiar with the common Data Structures and Algorithms in Python (English Edition) Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5C Programming Language, A Step By Step Beginner's Guide To Learn C Programming In 7 Days. Rating: 4 out of 5 stars4/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Algorithms For Dummies Rating: 4 out of 5 stars4/5Windows 11 For Dummies Rating: 0 out of 5 stars0 ratings
Reviews for Basic Core Python Programming
0 ratings0 reviews
Book preview
Basic Core Python Programming - Meenu Kohli
CHAPTER 1
Introduction
Introduction
You are all set to embark on a journey to learn Python, but before getting started you must learn about different types of programming languages, the history of Python, how it is different from other languages, and so on. This chapter is an introduction to Python Programming.
Structure
Overview of programming languages
High-level programming languages
Low-level programming languages
Machine language
Assembly language
Language processors
Assembler
Compiler
Interpreter
Generations of programming languages
Programming paradigms
History of Python
Features of Python
Future of Python
Installing Python in 5 simple steps
Memory management in Python
Python versus Java
Objectives
After reading this chapter you will:
Understand the difference between low-level and high-level programming languages
Know the difference between different types of language processors
Learn about generations of programming languages
Know about the history of Python
Learn about the features of Python
Understand the future of Python
Learn how to install Python on to your machine
Understand Python memory management
Understand the difference between Python and Java
Programming languages comprise of a set of rules that direct how commands, symbols, and syntax can be put together to write a piece of code that a computer can execute to carry out a particular task. Programs created using programming languages convey to the computer what needs to be done or what instructions must be carried out to perform a particular task.
1.1 Overview of programming languages
Programming languages are classified into two basic types as shown in figure 1.1:
High-level programming languages
Low-level programming languages
Figure 1.1
1.1.1 High-level programming languages
It is easy to write software programs in High-Level Languages (HLL) such as C, C++, Java, Python, and so on. Logically, a high-level language is very close to how we generally speak. The English instructions that we use in Java, Python, or any other HLL are very close to the English that we use for conversation. High-level languages have English words and mathematical symbols that are easily understood by coders. However, computer hardware can only understand the language of ‘0’s and ‘1’s and so it is a must to convert code written in the HLL Program to a format that a machine can understand. Hence, the code has to undergo compilation or interpretation before being executed by the computer. High-level languages are portable, which means that you can run the same code on different machines.
1.1.2 Low-level programming languages
Low-level programming languages can be easily interpreted and understood by a computer. A computer can easily understand the basic instructions of a low-level language. Making sense out of such programming language can be a difficult task for a normal human brain. Low-level programming languages are needed for writing programs for specific computer hardware or architecture. It is necessary to have extensive knowledge about a system’s hardware and its configuration to work with a low-level programming language. There are two types of low-level programming languages:
Machine language
Assembly language
Both machine language and assembly language are hardware-specific and are not portable.
1.1.2.1 Machine language
Machine language is the language of binary code - ‘0’s and ‘1’s. A computer can understand machine language directly, and no further translation is required. It is the lowest level of computer language. All instructions in machine language are in binary code so, 1100011100010101000011 can be a computer instruction. Software programmers never need to look at the machine language code. It is only viewed by those who build software compilers and operating systems. Machine languages used for programming in first-generation computers are called First Generation Language (1GL). CPU directly executes machine language program instructions; translation is not required. Hence, it is fast and efficient. However, fixing an error in machine language can be a very tedious job.
1.1.2.2 Assembly language
Assembly language is the Second Generation Language (2GL). It is a step above machine language, and a step closer to high-level language. It became popular in the 1950s because it used letters, symbols, and few simpler terms like ADD, MOV, SUB, and so on. These were simpler than using the complex series of ‘0’s and ‘1’s. However, a computer does not understand assembly language instructions unless converted to machine language using an assembler. Assembly language is quite complicated. It may be one step above machine language but not ideal for writing complex code.
1.2 Language processors
We all know that computers cannot process programming languages and instructions directly. A tool is required to convert/translate the code into machine language. The tools used for converting programming language (high-level language or assembly language) to machine code are called language processors or language translators. These language processors convert the code into binary code and inform the programmer if there is an error in the code. The language processors are of three types (Figure 1.2):
Assembler
Compiler
Interpreter
Although assembly language is machine-dependent yet, it has its own sets of instructions and is more programmer-friendly than machine-friendly. Just like high-level language, assembly language also requires the conversion of instructions to machine language. A program written ina high-level language is known as source code. The source code after getting converted to machine language is known as object code.
Figure 1.2: Language processor types
1.2.1 Assembler
If a program has its source code in assembly language, then an assembler is required to convert the source code to object code (binary). Once the object code is ready, the omputer can easily understand and execute the instructions.
Figure 1.3: How an assembler works
1.2.2 Compiler
A compiler reads the entire source code and translates it into machine code in one go. In case it encounters an error in the code (such as wrong syntax, characters, combination or sequencing of commands, and so on), it notifies the programmer about it. It displays all errors in the source code along with their location in the code. Source code with even one error will fail to compile.
After the errors in the code are fixed, you will have to compile the source code once again to generate the object code. It may happen that while translating the source code, the compiler creates an intermediate object code or a byte code, which is something between a high-level language and machine language. In such a case, an interpreter is required to convert the intermediate code to machine code.
Java programmers use the javac command to compile the source code. This command produces byte code or intermediate object code. This intermediate object code is converted into machine code with the help of Java Virtual Machine (JVM). The CPU then reads the instructions from the machine code and executes them one by one.
Figure 1.4: How a compiler works
1.2.3 Interpreter
The interpreter functions in a very different manner as compared to an assembler or a compiler. It translates the code one line at a time. While executing the code, if the interpreter encounters an error, it immediately terminates the translation process and displays an error message. As the instructions are interpreted and executed line by line, and not in one go, the interpreter does not generate any object code. Python is an Interpreted language.
Figure 1.5: How an interpreter works
1.3 Generations of programming languages
From machine language to high-level language, programming languages have come a long way in the last couple of decades. However, this development took place in phases and every phase introduced some new features that made programming easier than the previous phase. The programming languages can be grouped into five generations as shown in the following table:
Table 1.1: Generations of programming languages
1.4 Programming paradigms
Everything has a history associated with it. If you have a look at the history of computer science you will realize that from the time the foundation of computer programming was laid, the programmers have been putting in constant efforts to invent various ways or styles of programming known as a paradigm. So, a paradigm is more like a school of thought, that provides a framework or approach that can be used to implement a programming language.
Figure 1.6: Programming paradigm
There is a huge difference between a programming paradigm and a programming language. A programming paradigm is just a way of programming, whereas programming language comes with a defined set of vocabulary, rules, and instructions that must be followed properly to make the computer perform a certain task. There are a total of 27 paradigms out of which the four major ones are mentioned as follows:
Imperative/Procedural paradigm: The imperative procedural language uses a sequence of statements, and the state of the program changes as each statement is executed.
Object-oriented paradigm: In this programming paradigm, everything is modeled as an object. All the data and functions are bounded by an entity known as class. Object-oriented classes follow a modular approach and can be easily debugged and modified. Important features of object-oriented languages are:
Data abstraction
Encapsulation
Inheritance
Polymorphism
Functional paradigm: In functional programming, the computer functions are written in the form of a mathematical function. Idle for working in a multi-core, multi-threaded environment. It aims to resolve a problem by working on "what you want to resolve rather than
how you want to resolve it". Immutability is the main feature of this paradigm and it does not encourage state changing and mutable data.
Logical paradigm: This paradigm is based on formal logic, and can be quickly developed as it makes use of true or false statements.
1.5 History of Python
Python is a successor to the ABC language. The concept of Python was conceived in the late 80s and implemented in the early 90s. It was designed by Guido Van Rossum and developed by the Python Software Foundation. The main goal behind developing Python was to have a language that stressed code readability, the syntax of which allowed the coders to write a program in fewer lines of code.
Van Rossum, the principal author for Python named the language after the famous BBC TV Show – "Monty Python’s Flying Circus. He was given the title of
Benevolent Dictator For Life"(BDFL) by the Python community. On 12 July 2018, Rossum Stepped down as the language overlord leaving behind no successor or governance.
1.6 Features of Python
There is no doubt that Python is a very powerful language and it has some great features. It is interactive, dynamic, supports multiple programming paradigms, and so on. All these features make it a very reliable language, in addition to that its simplicity makes it fun to work with.
Python is a simple language
Python makes use of very simple English phrases.
It is much simpler to use as compared to languages like C, C++, or JAVA.
Python follows a very simple syntax structure.
There is no need to explicitly declare variables.
You can define complex operations in a single statement.
Instead of opening and closing brackets, Python uses indentation for statement grouping.
The syntax and programming structure followed while coding in Python is very simple.
Python is very easy to learn.
Python is a versatile or flexible language
Python is a flexible and versatile language because it can be used for many purposes.
Packages like NumPy, matplotlib, pandas, and so on make it a perfect programming language for data analysis, web development, artificial intelligence, data science, and much more.
Presently it is in great demand for application development and system development programming.
It is also a very suitable language for developing complex multi-protocol network applications.
Interpreted
Python code does not require explicit compilation.
It is an interpreted language where the code is converted to an intermediate code called bytecodes.
The bytecode is converted to a machine code that can be understood and executed by the computer.
Python allows easy debugging
Python is an interpreted language. It is executed line by line which makes it easier to debug.
Python is a free and open-source software
Python can be downloaded for free from its official website.
You can install Python on your system for free.
Python is freely used and distributed, and anyone can attribute to it.
You can also use Python for commercial purposes for free because it was developed under OSI – approved open source license.
Python is an example of Free Libre And Open Source Software (FLOSS).
Its license is administered by the Python Software Foundation.
Python interactive mode
The interactive mode of Python which is a Python prompt allows users to interact directly with the interpreter.
It allows the user to test the code before finalizing it which greatly helps in reducing errors at the time of coding.
Python is a high-level language
Python is a programmer-friendly language, which is why an interpreter is required to translate the code to machine language.
A Python coder only has to focus on the logic, and low-level processor operations are not something that a coder should be worried about.
Python is portable
Python code can be executed on any platform without making any alteration in the code. No initial setup of the software is required for executing a program.
Python is platform-independent
The software coded in Python can run on different operating environments. This means that a Python program created UNIX for instance, and then can be executed on a Windows system that has Python.
Python supports multiple programming paradigms
Python programming language supports multiple programming paradigms such as imperative/procedural, object-oriented, functional and logical paradigms.
Python is user friendly
Features like high-level data structures and extensive standard free libraries make Python user friendly.
The code can be divided into modules. These modules can be used in other applications thereby saving a lot of effort and time.
Python is a dynamic language
Python is called a dynamic language because a coder need not declare the data type of any variable in the code.
The data type of a variable is assigned dynamically when the value is stored in the variable.
Python is an extensible programming language
Python is an extensible programming language that allows ways or mechanisms to extend the programming language.
It allows usage of other languages in Python code.
You can interface Python with libraries written in other languages. The benefit of using an extension module is that when the code is executed these modules execute at the speed of compiled code rather than the speed of interpreted code. This feature allows for faster development.
Extensive libraries
Python has a vast collection of libraries. It offers a very rich set of functionality through its huge standard library.
These library functions are compatible with various operating systems such as Windows, Macintosh, UNIX, and so on.
Python is an embeddable language
Python code can be embedded in programs written in other languages such as C, C++ to enhance the quality of code.
Python is easy to maintain
Python code is very easy to maintain because it has clear and concise syntax, it is easy to read, and a lot can be accomplished in fewer lines of code as compared to C++ and Java.
Python is robust
Python is a robust language because it can do much more in much lines of code.
Python code is less prone to errors.
Python is easy to debug and is easy to maintain.
All these features make it a robust language.
Difference between extending and embedding
When you embed Python code in a program that is written in some other language (such as C or C++, and so on), you are inserting calls in the code to initialize the Python interpreter so that it can be called whenever the Python code in the application requires it.
On the other hand, when you extend the functionality of Python by including the code of some other language to your code written in Python, you need to first make sure that the code has been compiled into a shared library that is compatible with Python's interpreter. The interpreter can then load it as part of an import statement.
Database connectivity
Python provides support for all the the major databases that are commonly used in various applications.
Automatic garbage collection
This feature allows for great performance management due to the efficient usage of memory.
Programmers need not worry about memory leaks because any object that is no longer required goes out of scope, and is removed by the process of garbage collection.
1.7 Future of Python
You have already learned about some great features of the Python programming language. There several benefits of using Python.
Working with Python has cost benefits associated with it because it is free.
Python is a great language to be used for both front-end and back-end development projects.
It is platform-independent, it works on all operating environments in the same manner.
Python offers strong community support which means that if you face a problem