flowchart LR
A["Source Code (Python)<br>if count == 10:<br> print('Done')<br>else:<br> count += 1"]
B["Assembly Language<br>CMP R1, #10<br>BEQ label_done<br>ADD R1, R1, #1<br>label_done: CALL print"]
C["Machine Language<br>0011 0001 0000 1010<br>0100 0010 0001 0101<br>0010 0001 0001 0001<br>1000 0011 0010 0000"]
A --> B
B --> C
10 Introduction to Programming
Programming is about getting a computer to do what you (the user) want it to do. It’s about trying to make the future less painful. It’s about making things easier for our teammates. It’s about getting things wrong and being able to bounce back. It’s about forming good habits. It’s about understanding your toolset. The Pragmatic Programmer, 2020
Python is a programming language that is widely used in various applications, including web development, data analysis, artificial intelligence, and scientific computing. Python is a powerful tool that can help you automate tasks, create custom functions, and build complex applications.
But how does a programming language like Python work? What are the key concepts and features of Python that make it so popular? In this chapter, we will explore the basics of Python programming, including its syntax, data types, control structures, functions, and more.
10.1 How Programming Languages Work
Programming languages are used to write instructions that tell a computer what to do. These instructions are written in a specific syntax that the computer can understand and execute. When you write code in a programming language, you are essentially giving the computer a set of commands to follow.
Programming languages can be classified based on several criteria, including their level of abstraction, how they are executed, and how they handle data types. In the following, we will discuss some of the key characteristics of programming languages and how they apply to Python.
High-Level vs. Low-Level Languages
Programming languages can be classified into two main categories based on their level of abstraction and complexity:
- High-Level Languages: These languages are closer to human language and are easier to read and write. Examples include Python, Java, and JavaScript. These languages are designed to be user-friendly and abstract away the complexities of the underlying hardware.
- Low-Level Languages: These languages are closer to machine language and are more difficult to read and write. Examples include Assembly language and machine code. These languages are designed to interact directly with the hardware and are used for system-level programming. Their syntax is more complex and requires a deeper understanding of computer architecture.
Therefore, Python is a high-level language, allowing users to write code in a more human-readable format.
Interpreted vs. Compiled Languages
Programming languages can also be classified based on how they are executed by the computer:
- Interpreted Languages: In interpreted languages, the code is executed line by line by an interpreter. The interpreter reads the code, translates it into machine code, and executes it immediately. Examples include Python, JavaScript, and Ruby.
- Compiled Languages: In compiled languages, the code is translated into machine code by a compiler before execution. The compiler reads the entire code, checks for errors, and generates an executable file that can be run independently of the source code. Examples include C and C++.
- Hybrid Languages: Some languages, such as Java, use a combination of compilation and interpretation. The code is compiled into an intermediate bytecode, which is then interpreted or just-in-time compiled at runtime.
Therefore, Python is an interpreted language.
A compiler is a program that translates source code written in a high-level programming language (such as VBA) into machine code that can be executed by the computer. The compiler checks the syntax of the code and generates an executable program if there are no errors.
An interpreter is a program that executes instructions written in a programming language. It reads the source code line by line, translates it into machine code, and executes it immediately. This allows for more flexibility and ease of debugging.
Strongly Typed vs. Weakly Typed Languages
Programming languages can also be classified based on how they handle data types:
- Strongly Typed Languages: In strongly typed languages, variables must be declared with a specific data type, and the type of a variable cannot change during execution. This helps prevent errors and ensures that variables are used correctly. Examples include Java, C++, and C.
- Weakly Typed Languages: In weakly typed languages, variables do not need to be declared with a specific data type, and the type of a variable can change during execution. This can lead to errors if variables are used incorrectly. Examples include JavaScript, PHP, and Python.
Python is a weakly typed language, meaning that you don’t need to declare the data type of variables explicitly, and the type is determined at runtime.
Static vs. Dynamic Typing
Programming languages can also be classified based on how they handle data types at compile time and runtime:
- Static Typing: In statically typed languages, the data type of variables is checked at compile time. This helps catch errors early and ensures that variables are used correctly. Examples include Java, C++, and C#.
- Dynamic Typing: In dynamically typed languages, the data type of variables is checked at runtime. This allows for more flexibility but can lead to errors if variables are used incorrectly. Examples include Python, JavaScript, and Ruby.
Python is a dynamically typed language, meaning that the data type of variables is determined at runtime.
The compile time is the phase in which the source code is translated into machine code by a compiler. During this phase, the compiler checks the syntax of the code and generates an executable program if there are no errors. The run-time is the phase in which the executable program is executed by the computer. During this phase, the program reads input, processes data, and produces output.
A variable is a named storage location in memory that holds a value. Variables are used to store data that can be manipulated and changed during program execution. Variables have a name, a data type, and a value. By using variables, you can store and work with different types of data in your programs.
You can think of a variable as a container that holds a value. It is a bin or a box that you can put things in and take things out of. For example, you can create a variable called counter to store a number, or a variable called message to store text. Variables are an essential concept in programming and are used to store and manipulate data in your code.
A data type is a classification that specifies which type of value a variable can hold. Data types define the size, range, and operations that can be performed on the variable. For example, an integer data type can hold whole numbers, while a string data type can hold text. Data types help ensure that variables are used correctly and efficiently in a program.