INTRODUCTION TO PYTHON PROGRAMMING COURSE
- Introduction to Python
- Python Basics (syntax, variables, data types)
- Python Operators
- Python If…Else
- Python While Loops
- Python For Loops
- Python Functions
- Python Lambda
- Python Arrays
- Python Classes and Objects
Introduction to Computers and Their Architecture
A computer is an electronic device that can perform various tasks by executing instructions given to it. It processes data under the control of a program, which is a set of instructions stored in its memory. Computers come in various forms, from small embedded systems to large mainframe computers, but they all share common basic components.
Basic Components of a Computer
Central Processing Unit (CPU): The CPU is the brain of the computer, responsible for executing instructions and controlling the operation of the other components. It consists of an arithmetic logic unit (ALU) for performing calculations and a control unit for managing the execution of instructions.
Memory: Computers use memory to store data and instructions temporarily while they are being processed. There are two main types of memory: RAM (Random Access Memory), which is volatile and loses its contents when the power is turned off, and ROM (Read-Only Memory), which is non-volatile and retains its contents even when the power is off.
Input Devices: Input devices allow users to interact with the computer and provide it with data and instructions. Common input devices include keyboards, mice, and touchscreens.
Output Devices: Output devices display the results of computations and allow users to see or hear the output. Common output devices include monitors, printers, and speakers.
Storage Devices: Storage devices are used to store data and programs permanently. Examples include hard disk drives (HDDs) and solid-state drives (SSDs).
Computer Architecture
Computer architecture refers to the design and organization of a computer system, including its hardware and software components. It encompasses the following key aspects:
Instruction Set Architecture (ISA): The ISA defines the set of instructions that a computer can execute and how they are encoded. It also includes the registers and memory addressing modes used by the CPU.
Processor Design: Processor design involves designing the CPU, including the ALU, control unit, and registers. It also includes designing the instruction pipeline and cache memory for improving performance.
Memory Hierarchy: The memory hierarchy refers to the organization of memory in a computer system, from fast but small caches to slower but larger main memory and storage devices. This hierarchy is designed to optimize performance and cost.
Input/Output (I/O) System: The I/O system manages the transfer of data between the computer and external devices. It includes controllers for different types of devices and mechanisms for data transfer.
Computers are complex systems with a wide range of components and functionalities. Understanding their architecture is essential for understanding how they work and how to program them effectively.
Introduction to Operating Systems and How They Work
An operating system (OS) is a software that acts as an intermediary between computer hardware and the user. It provides a platform for running applications and manages the computer’s resources, such as memory, processors, and input/output devices. Without an operating system, a computer would be unable to perform tasks or run programs effectively.
Functions of an Operating System
Resource Management: The OS manages the computer’s resources, including memory, processors, and input/output devices. It allocates resources to running programs and ensures that they are used efficiently.
Process Management: The OS manages processes, which are instances of running programs. It handles the creation, scheduling, and termination of processes, ensuring that each process gets the resources it needs.
Memory Management: The OS manages the computer’s memory, ensuring that each program has enough memory to run and that memory is allocated and deallocated efficiently.
File System Management: The OS manages files on the computer’s storage devices, such as hard drives and solid-state drives. It handles file creation, deletion, and organization, providing a way for users to store and retrieve data.
Device Management: The OS manages input/output devices, such as keyboards, mice, printers, and monitors. It handles communication between devices and programs, ensuring that data is transferred correctly.
User Interface: The OS provides a user interface through which users can interact with the computer. This can be a graphical user interface (GUI) or a command-line interface (CLI), depending on the OS.
How an Operating System Works
Boot Process: When you turn on a computer, the OS is loaded into memory through a process called bootstrapping or booting. The boot process initializes the computer’s hardware and loads the OS kernel into memory.
Kernel: The kernel is the core of the operating system, responsible for managing the computer’s resources and providing a platform for running applications. It interacts directly with the hardware and provides services to other parts of the OS and to applications.
System Calls: Applications communicate with the OS through system calls, which are requests for OS services. For example, when an application wants to read from a file, it makes a system call to the OS’s file system management component.
Process Management: The OS manages processes by scheduling them to run on the CPU. It ensures that each process gets a fair share of the CPU’s time and that processes do not interfere with each other.
Memory Management: The OS manages memory by allocating memory to processes and ensuring that processes do not access memory that does not belong to them. It also handles virtual memory, which allows processes to use more memory than is physically available.
File System Management: The OS manages files on storage devices by providing a file system that organizes and stores files. It handles file operations, such as creation, deletion, and modification, and ensures that files are stored and retrieved correctly.
Device Management: The OS manages input/output devices by providing device drivers that control the devices. It handles device communication and ensures that data is transferred correctly between devices and programs.
User Interface: The OS provides a user interface through which users can interact with the computer. This can be a graphical user interface (GUI) with windows, icons, and menus, or a command-line interface (CLI) where users type commands.
An operating system is a crucial component of a computer system, providing a platform for running applications and managing the computer’s resources. It performs various functions, including resource management, process management, memory management, file system management, device management, and providing a user interface. Understanding how an operating system works is essential for effectively using and managing computer systems.
Introduction to Programming Languages:
Low-Level and High-Level
Programming languages are used to write instructions for computers to execute. They vary in complexity and abstraction, ranging from low-level languages that are close to machine code to high-level languages that are more human-readable. Here’s an overview of low-level and high-level programming languages with examples:
Low-Level Programming Languages
Low-level languages are closer to the hardware and are more difficult for humans to read and write compared to high-level languages. They provide more direct control over the computer’s hardware and are often used in system programming and embedded systems. There are two main types of low-level languages:
Machine Language: Machine language is the lowest level of programming language and is directly executable by the computer’s CPU. It consists of binary code (0s and 1s) that represent instructions and data. Here’s an example of machine language instructions:
1 |
10110000 01100001 |
Assembly Language: Assembly language is a more human-readable version of machine language that uses mnemonics to represent instructions. Each mnemonic corresponds to a specific machine language instruction. Here’s an example of assembly language instructions:
assembly
1 2 |
MOV AX, 5 ADD AX, BX |
High-Level Programming Languages
High-level languages are more abstract and easier for humans to read and write compared to low-level languages. They provide higher levels of abstraction and are closer to natural language. High-level languages are typically compiled or interpreted into machine code before execution. Some examples of high-level programming languages include:
Python: Python is a high-level, interpreted language known for its readability and simplicity. It is widely used in web development, data analysis, artificial intelligence, and scientific computing. Here’s an example of Python code:
python
1 |
print("Hello, world!") |
Java: Java is a high-level, compiled language that is platform-independent, meaning it can run on any device with a Java Virtual Machine (JVM). It is commonly used in enterprise applications, mobile development, and web development. Here’s an example of Java code:
java
1 2 3 4 5 |
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } } |
C++: C++ is a high-level, compiled language that is an extension of the C programming language. It is used for system programming, game development, and software development. Here’s an example of C++ code:
cpp
1 2 3 4 5 6 7 |
#include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } |
High-level languages are preferred for most programming tasks due to their readability, ease of use, and portability. They allow developers to focus on solving problems rather than dealing with low-level details of the computer’s hardware.
welcome to python proigramming. here we shall look at how to get strated with python programming. we shall begin from thee basics and navigate all the way up. our main goal is to create a django project using the django gframework
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"""Welcome to Python programming. Here, we will look at how to get started with Python programming.</span></span> <span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="st">We shall begin with the basics and navigate all the way up.</span></span> <span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="st">Our main goal is to create a Django project using the Django framework."""</span>)</span> <span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="co">#the reason for using the three quotation signs is to show that the string is propery terminated</span></span> <span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="co">#alternatively one can use</span></span> <span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"/n"</span>)</span> <span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Welcome to Python programming."</span></span> <span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="st">" Here, we will look at how to get started with Python programming."</span></span> <span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a><span class="st">"We shall begin with the basics and navigate all the way up."</span></span> <span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a><span class="st">"Our main goal is to create a Django project using the Django framework."</span>)</span> <span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"/n""/n""/n"</span>)</span> <span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a><span class="co">#note that in the normal python ide such as pycharm the whole sentence would have been in one single double quote</span></span> <span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a></span> <span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a></span> <span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a></span> <span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a><span class="co"># this is the first python project</span></span> |
1 2 3 4 5 6 7 |
Welcome to Python programming. Here, we will look at how to get started with Python programming. We shall begin with the basics and navigate all the way up. Our main goal is to create a Django project using the Django framework. /n Welcome to Python programming. Here, we will look at how to get started with Python programming.We shall begin with the basics and navigate all the way up.Our main goal is to create a Django project using the Django framework. /n/n/n |
1 2 3 4 5 |
<span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"So what is python?"</span></span> <span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="st">"Python is a hier level programming language that is a multidimensional language and ius used in varoius fields"</span></span> <span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="st">"Python is a widely used general-purpose, high-level programming language. "</span></span> <span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="st">"It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. "</span></span> <span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="st">"It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code."</span>)</span> |
1 2 |
So what is python?Python is a hier level programming language that is a multidimensional language and ius used in varoius fieldsPython is a widely used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. |
Who invented Python?
In the late 1980s, history was about to be written. It was that time when working on Python started. Soon after that, Guido Van Rossum began doing its application-based work in December of 1989 at Centrum Wiskunde & Informatica (CWI) which is situated in the Netherlands. It was started as a hobby project because he was looking for an interesting project to keep him occupied during Christmas.
The programming language in which Python is said to have succeeded is ABC Programming Language, which had interfacing with the Amoeba Operating System and had the feature of exception handling. He had already helped create ABC earlier in his career and had seen some issues with ABC but liked most of the features. After that what he did was very clever. He had taken the syntax of ABC, and some of its good features. It came with a lot of complaints too, so he fixed those issues completely and created a good scripting language that had removed all the flaws. The inspiration for the name came from the BBC’s TV Show – ‘Monty Python’s Flying Circus’, as he was a big fan of the TV show and also he wanted a short, unique and slightly mysterious name for his invention and hence he named it Python! He was the “Benevolent dictator for life” (BDFL) until he stepped down from the position as the leader on 12th July 2018. For quite some time he used to work for Google, but currently, he is working at Dropbox.
Evolution of Python The language was finally released in 1991. When it was released, it used a lot fewer codes to express the concepts, when we compare it with Java, C++ & C. Its design philosophy was quite good too. Its main objective is to provide code readability and advanced developer productivity. When it was released, it had more than enough capability to provide classes with inheritance, several core data types of exception handling and functions. Following are the illustrations of different versions of Python along with the timeline.
Python is a versatile and widely-used programming language known for its readability and simplicity. It is used in a variety of domains and applications, including:
Web Development: Python’s frameworks like Django and Flask enable developers to build scalable web applications quickly.
Data Science and Machine Learning: Python’s libraries such as NumPy, pandas, scikit-learn, and TensorFlow are extensively used for data analysis, machine learning, and artificial intelligence applications.
Scripting: Python is often used for scripting tasks due to its concise syntax and readability, making it suitable for automation, system administration, and other scripting tasks.
Scientific Computing: Python is used in scientific computing and numerical simulations due to its libraries like SciPy and matplotlib, which facilitate data visualization and computation.
Education: Python’s readability and ease of use make it a popular choice for teaching programming concepts and computer science.
Desktop GUI Applications: Python can be used to create desktop GUI applications using libraries like Tkinter, PyQt, and wxPython.
Game Development: Python is used in game development, either directly through libraries like Pygame or as a scripting language within game engines like Unity and Unreal Engine.
Networking: Python’s standard library provides modules for networking tasks, making it suitable for developing network applications and protocols.
Embedded Systems: Python can be used for programming embedded systems, especially in situations where ease of development and rapid prototyping are crucial.
Testing: Python is used in testing frameworks like Pytest for writing and executing test cases.
Jobs and Self-Employment Opportunities in Python
Python is a versatile programming language with a wide range of applications, making it well-suited for various job opportunities and self-employment ventures. Here are some potential career paths and opportunities in Python:
1. Software Development
Python is widely used in software development for web development (Django, Flask), scientific computing, data analysis, artificial intelligence, and machine learning. You can work as a Python developer, building applications and systems for various industries.
2. Data Science and Analytics
Python is the go-to language for data science and analytics due to its rich ecosystem of libraries (NumPy, pandas, scikit-learn). You can work as a data scientist, data analyst, or machine learning engineer, analyzing data, building models, and deriving insights.
3. Web Development
Python is used for both front-end and back-end web development. With frameworks like Django and Flask, you can build web applications, APIs, and websites. You can work as a full-stack developer, specializing in Python-based web development.
4. DevOps and Automation
Python is popular in DevOps for infrastructure automation, configuration management (using tools like Ansible), and continuous integration/continuous deployment (CI/CD) pipelines. You can work as a DevOps engineer, automating and streamlining software development processes.
5. Game Development
Python is used in game development for scripting, AI programming, and prototyping. You can work as a game developer, using Python with game engines like Unity or Pygame to develop games.
6. Education and Training
With Python’s simplicity and readability, it’s a popular choice for teaching programming. You can work as a Python instructor, creating educational materials and courses for beginners and advanced learners.
7. Freelancing and Consulting
Python developers often work as freelancers or consultants, offering their expertise to clients on specific projects. This allows for flexibility in work hours and projects.
8. Entrepreneurship
Python developers can start their own businesses, offering software development services, developing products, or creating software solutions for specific industries or problems.
9. Open Source Contribution
Contributing to open-source projects in Python is a great way to build your skills, network with other developers, and contribute to the community.
Python offers a wide range of career opportunities and avenues for self-employment, thanks to its versatility and popularity in various industries. Whether you choose to work for a company or start your own venture, Python skills can open up a world of possibilities.
Introduction to Compilers, Interpreters, and IDEs
When you write code, you typically use a programming language that humans can understand. However, computers require instructions in a different format. This is where compilers and interpreters come in. They are tools that convert human-readable code into machine-readable instructions. Additionally, Integrated Development Environments (IDEs) are software applications that provide comprehensive facilities to computer programmers for software development. Let’s explore these concepts in more detail.
Compilers
A compiler is a program that translates source code written in a high-level programming language into a lower-level language, often machine code. The compiler performs this translation in a single step, producing an executable file that can be run independently of the compiler. Examples of compiled languages include C, C++, and Rust.
- Compilation Process:
- The compiler scans the entire program and translates it as a whole into machine code.
- It generates an executable file that can be run on a specific platform.
- Advantages:
- Generally, compiled programs are faster than interpreted ones since the translation only happens once.
- Compilers can catch syntax errors early in the development process.
- Disadvantages:
- Compilation can be time-consuming, especially for large programs.
- Compiled programs are platform-dependent and may require recompilation for different platforms.
Interpreters
An interpreter, on the other hand, translates code into machine code line by line, executing each line immediately after it is translated. Interpreted languages include Python, Ruby, and JavaScript.
- Execution Process:
- The interpreter translates and executes the code line by line.
- There is no separate compilation step, and the code is executed directly from the source.
- Advantages:
- Interpreted languages are generally more portable since the interpreter can run on different platforms.
- Interpreters are useful for rapid development and prototyping.
- Disadvantages:
- Interpreted programs are generally slower than compiled ones since each line of code is translated at runtime.
- Errors may only be discovered during execution, making debugging more challenging.
Integrated Development Environments (IDEs)
An IDE is a software application that provides a comprehensive set of tools for software development. It typically includes a source code editor, build automation tools, and a debugger, among other features. Examples of popular IDEs include Visual Studio Code, PyCharm, and Eclipse.
- Features:
- Code editor with syntax highlighting and auto-completion.
- Built-in debugger for identifying and fixing errors in code.
- Build automation tools for compiling and running code.
- Version control integration for managing code changes.
- Advantages:
- IDEs provide a unified environment for all aspects of development, improving productivity.
- They often include features like code refactoring and unit testing, making it easier to write high-quality code.
- Disadvantages:
- IDEs can be resource-intensive, especially for large projects.
- Beginners may find the multitude of features overwhelming.
In conclusion, compilers and interpreters are essential tools for translating human-readable code into machine-readable instructions, with each having its advantages and disadvantages. IDEs, on the other hand, provide a comprehensive environment for software development, integrating various tools to streamline the development process.
Getting started with python
1 2 |
<span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co">#printing your first python program</span></span> <span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Hello, World!"</span>)</span> |
1 2 |
Hello, World! |
Python identation: Here we shall talk about the python syntax, in python we don’t use the semicolon to show the end of a statement, instead wqe use what we call python identation. We shall look at an example below.
1 2 3 4 5 |
<span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">23</span> <span class="co">#this is the variable x which is an integer</span></span> <span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="dv">200</span> <span class="co">#This is the variable Y which is an integer also</span></span> <span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> x<span class="op"><=</span> y:<span class="co"># This is a while loop</span></span> <span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(x, end<span class="op">=</span><span class="st">", "</span>)<span class="co">#this is the print statement to output the answer of the loop</span></span> <span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> x<span class="op">=</span>x<span class="op">+</span><span class="dv">1</span> <span class="co"># this is the variable x undergoing an increament</span></span> |
1 |
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, |
1 2 3 4 5 6 7 8 |
<span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">20</span></span> <span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="dv">200</span></span> <span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> x<span class="op"><=</span>y:</span> <span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(x, end<span class="op">=</span><span class="st">", "</span>)</span> <span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> x<span class="op">+=</span><span class="dv">1</span></span> <span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> x <span class="op">==</span> <span class="dv">199</span>:</span> <span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">pass</span></span> <span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(x)</span> |
1 2 |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199 199, 200, |
1 2 3 4 5 6 7 8 |
<span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">300</span></span> <span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="dv">20</span></span> <span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> y<span class="op"><=</span>x:</span> <span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(x)</span> <span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a> y<span class="op">+=</span><span class="dv">1</span></span> <span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> y <span class="op">==</span> <span class="dv">25</span>:</span> <span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(y)</span> <span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span></span> |
1 2 3 4 5 6 7 |
300 300 300 300 300 25 |
1 2 3 4 5 6 7 8 |
<span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="dv">300</span></span> <span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">600</span></span> <span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> y<span class="op"><=</span><span class="dv">600</span>:</span> <span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a> <span class="co">#print(y, end=", ")</span></span> <span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a> y <span class="op">+=</span><span class="dv">1</span></span> <span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> y<span class="op">==</span><span class="dv">320</span>:</span> <span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(y)</span> <span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">continue</span></span> |
1 2 |
320 |
1 2 3 4 5 6 7 8 |
<span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>x <span class="op">=</span> <span class="dv">300</span></span> <span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>y <span class="op">=</span> <span class="dv">20</span></span> <span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> y <span class="op"><=</span> x:</span> <span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(x)</span> <span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> y <span class="op">+=</span> <span class="dv">1</span></span> <span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> y <span class="op">==</span> <span class="dv">25</span>:</span> <span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(y) <span class="co"># This will print y when it reaches 25</span></span> <span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span></span> |
1 2 3 4 5 6 7 |
300 300 300 300 300 25 |
the above are some of the common examples that use python identation. THE EXAMPLES ABOVE COVER A WHILE LOOP WITH SOME conditiuons(pass, break anmd continue) these are added so that the while loop is altered in the way it runs the code.
1 2 |
<span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="dv">5</span><span class="op">></span><span class="dv">3</span>:</span> <span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'5 is greator than 3'</span>)</span> |
1 2 |
5 is greator than 3 |
The above code also illustraytes the impact of python identation but now this case is of using the if statement
1 2 3 4 5 |
<span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co">#this is a single lionbe comment in python</span></span> <span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="co">''' this is a multiline comment in python'''</span></span> <span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="co">"""</span></span> <span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="co">This is also a mul;tiline comment in python</span></span> <span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a><span class="co">"""</span></span> |
1 |
<span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span><span class="dt">"type"</span><span class="fu">:</span><span class="st">"string"</span><span class="fu">}</span></span> |
The above code shows comments in python. comments are usefulas they hjelp the programmers understand each others code. They also work as reminder notes that is if some
1 2 |
<span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co">#print("Hello, World!")</span></span> <span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Cheers, Mate!"</span>)</span> |
1 2 |
Cheers, Mate! |
Inthe above example you notice the one with a preceeding hash is not printed out as it is read as a comment
1 2 3 4 5 6 |
<span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="co">"""</span></span> <span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="co">This is a comment</span></span> <span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="co">written in</span></span> <span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a><span class="co">more than just one line</span></span> <span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a><span class="co">"""</span></span> <span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Hello, World!"</span>)</span> |
1 2 |
Hello, World! |
In the above cell you notice that the sentence with the three quotation marks is not printed out as it is read as a comment
1 2 3 4 5 |
<span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>w<span class="op">=</span><span class="st">'string'</span></span> <span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">23</span></span> <span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="fl">12.3</span></span> <span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a>z<span class="op">=</span><span class="ot">12j</span></span> <span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x,y,z)</span> |
1 2 |
23 12.3 12j |
The above describes how you can create varibles and also show their oputput. In the above example that is how we create variables. In the cells below we shall discuss how we create casting of vaiables, this is used to convert a given datatype of form ‘A’ to form ‘B’
1 2 3 4 5 6 7 8 9 10 11 |
<span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="co">#example</span></span> <span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a><span class="co"># in this case we shall discuss how the variable type changes from form a to form b</span></span> <span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">12</span></span> <span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="fl">12.5</span></span> <span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a>z<span class="op">=</span><span class="ot">12j</span></span> <span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="bu">type</span>(x), <span class="bu">type</span>(y), <span class="bu">type</span>(z))</span> <span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></a><span class="co">#now perfor5ming casting of the variables</span></span> <span id="cb32-8"><a href="#cb32-8" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="bu">float</span>(x)</span> <span id="cb32-9"><a href="#cb32-9" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="bu">complex</span>(y)</span> <span id="cb32-10"><a href="#cb32-10" aria-hidden="true" tabindex="-1"></a>z<span class="op">=</span><span class="bu">str</span>(z)</span> <span id="cb32-11"><a href="#cb32-11" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="bu">type</span>(x), <span class="bu">type</span>(y), <span class="bu">type</span>(z))</span> |
1 2 3 |
<class 'int'> <class 'float'> <class 'complex'> <class 'float'> <class 'complex'> <class 'str'> |
Note when assinging variables to strings you can either use single or double Quotes
1 2 3 4 5 |
<span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>a<span class="op">=</span><span class="dv">12</span></span> <span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>A<span class="op">=</span><span class="ot">12j</span></span> <span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(a)</span> <span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(A)</span> <span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a><span class="co">#note that python is case sensitive</span></span> |
1 2 3 |
12 12j |
The above example tries to emphasize on the importance of case sensitivity in python programming
1 2 3 4 5 6 |
<span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="co">#lets see how you assign names to variables</span></span> <span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>My_name<span class="op">=</span><span class="st">'John'</span></span> <span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a>age<span class="op">=</span><span class="dv">25</span></span> <span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a>myAge<span class="op">=</span><span class="dv">25</span></span> <span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a>_myAge<span class="op">=</span><span class="dv">25</span></span> <span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">'Those are the examples of variable names and how theyt are named'</span>)</span> |
1 2 |
Those are the examples of variable names and how theyt are named |
Discussion: Naming Variables
When naming variables in Python, it’s essential to follow certain rules and conventions to ensure clarity and readability in your code. Here are the guidelines for naming variables in Python:
Variable Names
A variable can have a short name (like x
and y
) or a more descriptive name (age
, carname
, total_volume
).
Rules for Python Variables:
- Start with a Letter or Underscore:
- A variable name must start with a letter (a-z, A-Z) or the underscore character (_).
- Cannot Start with a Number:
- A variable name cannot start with a number (0-9).
- Contain Alpha-Numeric Characters and Underscores:
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
- Case-Sensitive:
- Variable names are case-sensitive (
age
,Age
, andAGE
are three different variables).
- Variable names are case-sensitive (
- Avoid Python Keywords:
- A variable name cannot be any of the Python keywords (
if
,else
,for
,while
, etc.).
- A variable name cannot be any of the Python keywords (
Following these rules and conventions will make your code more readable and maintainable, improving collaboration and understanding among developers.
1 2 3 4 5 6 7 |
<span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co">#camel case</span></span> <span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a>myNameIs<span class="op">=</span><span class="st">'Charles'</span></span> <span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a><span class="co">#snake case</span></span> <span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a>my_name_is<span class="op">=</span><span class="st">'Charles'</span></span> <span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a><span class="co">#Pascals case</span></span> <span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a>MyNameIs<span class="op">=</span><span class="st">'charles'</span></span> <span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(myNameIs,my_name_is, MyNameIs )</span> |
1 2 |
Charles Charles charles |
Discussion: Naming Variables in Different Styles
When naming variables in programming, it’s common to use different styles to improve readability and maintainability. Three common styles are Pascal case, Camel case, and Snake case.
Pascal Case
- Definition:
- Pascal case, also known as Upper Camel Case, capitalizes the first letter of each word and removes spaces.
- Example:
FirstName
,LastName
,PhoneNumber
.
Camel Case
- Definition:
- Camel case, also known as Lower Camel Case, is similar to Pascal case but starts with a lowercase letter.
- Example:
firstName
,lastName
,phoneNumber
.
Snake Case
- Definition:
- Snake case uses underscores (_) between words and is often preferred in Python for naming variables and functions.
- Example:
first_name
,last_name
,phone_number
.
Choosing the right naming style depends on the programming language and coding conventions of the project. In Python, snake case is commonly used for variables and functions, while Pascal case and Camel case are more prevalent in languages like C# and Java.
1 2 3 4 5 |
<span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>x,y,z<span class="op">=</span><span class="st">'orange'</span>,<span class="st">'mango'</span>,<span class="st">'strawberry'</span></span> <span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> <span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(y)</span> <span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(z)</span> <span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a><span class="co">#this is the way we assign multiple variables to values then the way we print them can be assocviated with unpacking of variables</span></span> |
1 2 3 4 |
orange mango strawberry |
1 2 3 4 5 6 |
<span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>y<span class="op">=</span>z<span class="op">=</span><span class="dv">200</span></span> <span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a><span class="co">#here we have assigned 200 to all the variables x, y,z</span></span> <span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a><span class="co">#we expect that if we print x, y or z we have 200 as the output</span></span> <span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> <span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(y)</span> <span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(z)</span> |
1 2 3 4 |
200 200 200 |
the above code illustrates a good way of how you can assign a single bvalue to multiple variables
1 2 3 4 5 6 7 |
<span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>fruit<span class="op">=</span>[<span class="st">'orange'</span>,<span class="st">'mango'</span>,<span class="st">'watermelon'</span>,<span class="st">'passion fruit'</span>]</span> <span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a>w,x,y,z<span class="op">=</span>fruit</span> <span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> <span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(y)</span> <span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(z)</span> <span id="cb44-6"><a href="#cb44-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(w)</span> <span id="cb44-7"><a href="#cb44-7" aria-hidden="true" tabindex="-1"></a><span class="co">#the above is what we call unpacking of a collection</span></span> |
1 2 3 4 5 |
mango watermelon passion fruit orange |
In the above code we see how a collection which uses the list data type can be unpacked. this is done by assigning the variables to the list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a></span> <span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> animal:</span> <span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, wild, domestic):</span> <span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.wild <span class="op">=</span> wild</span> <span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.domestic<span class="op">=</span> domestic</span> <span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> monkey(animal):</span> <span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, domestic, tail):</span> <span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a> <span class="bu">super</span>().<span class="fu">__init__</span>(<span class="va">False</span>, domestic)</span> <span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.tail<span class="op">=</span> tail</span> <span id="cb46-10"><a href="#cb46-10" aria-hidden="true" tabindex="-1"></a></span> <span id="cb46-11"><a href="#cb46-11" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> classification(<span class="va">self</span>):</span> <span id="cb46-12"><a href="#cb46-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="va">self</span>.domestic:</span> <span id="cb46-13"><a href="#cb46-13" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'Monkey is Domestic'</span>)</span> <span id="cb46-14"><a href="#cb46-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>:</span> <span id="cb46-15"><a href="#cb46-15" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'monkey is wild'</span>)</span> <span id="cb46-16"><a href="#cb46-16" aria-hidden="true" tabindex="-1"></a></span> <span id="cb46-17"><a href="#cb46-17" aria-hidden="true" tabindex="-1"></a>monkey<span class="op">=</span>monkey(<span class="va">True</span>, <span class="va">True</span>)</span> <span id="cb46-18"><a href="#cb46-18" aria-hidden="true" tabindex="-1"></a>monkey.classification()</span> |
1 2 |
Monkey is Domestic |
the above code is used to show how the python outputs it’s variables. variables can be outputed using print() function
1 2 3 |
<span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">23</span></span> <span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="dv">35</span></span> <span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x<span class="op">+</span>y)</span> |
1 2 |
58 |
The above code is used to print the output of x and y which will print the sum note if you tried to add the sum of an integer and a string it would not work
1 2 3 |
<span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">20</span></span> <span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="st">'years'</span></span> <span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x, y)</span> |
1 2 |
20 years |
The above cell shows how the output of many variables can be printed out
1 2 3 4 5 6 7 8 9 |
<span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">23</span></span> <span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> myfunc():</span> <span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">global</span> y</span> <span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a> y<span class="op">=</span><span class="dv">200</span></span> <span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f'we want to make </span><span class="sc">{</span>y<span class="sc">}</span><span class="ss"> the global variable'</span>)</span> <span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> getting_global_variable():</span> <span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f'the global variable </span><span class="sc">{</span>y<span class="sc">}</span><span class="ss">'</span>)</span> <span id="cb52-8"><a href="#cb52-8" aria-hidden="true" tabindex="-1"></a>myfunc()</span> <span id="cb52-9"><a href="#cb52-9" aria-hidden="true" tabindex="-1"></a>getting_global_variable()</span> |
1 2 3 |
we want to make 200 the global variable the global variable 200 |
from the above example we have made y the global variable that is considering that y is also in the fuinction above myfunc(), the making og global variable has occured by using the global keyword
1 2 3 4 5 6 7 8 9 10 11 |
<span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">23</span></span> <span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="dv">24</span></span> <span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a></span> <span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> area():</span> <span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> x<span class="op">*</span>y</span> <span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a></span> <span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> perimeter():</span> <span id="cb54-8"><a href="#cb54-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="dv">2</span><span class="op">*</span>x<span class="op">+</span><span class="dv">2</span><span class="op">*</span>y</span> <span id="cb54-9"><a href="#cb54-9" aria-hidden="true" tabindex="-1"></a></span> <span id="cb54-10"><a href="#cb54-10" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(area())</span> <span id="cb54-11"><a href="#cb54-11" aria-hidden="true" tabindex="-1"></a>perimeter()</span> |
1 2 |
552 |
1 |
94 |
In the above code we can see that both x and y are global variables and are being used by the 2 functions area() and perimeter()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>a<span class="op">=</span><span class="st">'string'</span><span class="co">#string data type</span></span> <span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a>b<span class="op">=</span><span class="dv">12</span><span class="co">#integer data type</span></span> <span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a>c<span class="op">=</span><span class="fl">12.90</span><span class="co">#float datatype</span></span> <span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a>d<span class="op">=</span><span class="ot">12j</span><span class="co"># complex numbers data type</span></span> <span id="cb57-5"><a href="#cb57-5" aria-hidden="true" tabindex="-1"></a>e<span class="op">=</span>[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="fl">66.77</span>,<span class="ot">78j</span>,<span class="st">'string'</span>]<span class="co"># list data type</span></span> <span id="cb57-6"><a href="#cb57-6" aria-hidden="true" tabindex="-1"></a>f<span class="op">=</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="fl">344.56</span>,<span class="ot">45j</span>,<span class="st">'string'</span>)<span class="co">#tuple data type</span></span> <span id="cb57-7"><a href="#cb57-7" aria-hidden="true" tabindex="-1"></a>g<span class="op">=</span><span class="bu">frozenset</span>({<span class="dv">1</span>,<span class="dv">2</span>,<span class="fl">344.56</span>,<span class="st">'string'</span>})<span class="co">#frozen set data type</span></span> <span id="cb57-8"><a href="#cb57-8" aria-hidden="true" tabindex="-1"></a>h<span class="op">=</span>{<span class="st">'one'</span>:<span class="fl">1.1</span>,<span class="st">'two'</span>:<span class="ot">2j</span>,<span class="st">'three'</span>:<span class="st">'three'</span>,<span class="st">'four'</span>:[<span class="dv">4</span>,<span class="dv">4</span>,<span class="dv">4</span>,]}<span class="co">#dictionary data type</span></span> <span id="cb57-9"><a href="#cb57-9" aria-hidden="true" tabindex="-1"></a>i<span class="op">=</span>{<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>}<span class="co">#set data type</span></span> <span id="cb57-10"><a href="#cb57-10" aria-hidden="true" tabindex="-1"></a>j<span class="op">=</span><span class="va">True</span><span class="co">#boolean data type</span></span> <span id="cb57-11"><a href="#cb57-11" aria-hidden="true" tabindex="-1"></a>k<span class="op">=</span><span class="bu">bytearray</span>(<span class="dv">2</span>)<span class="co">#byte array data type</span></span> <span id="cb57-12"><a href="#cb57-12" aria-hidden="true" tabindex="-1"></a>l<span class="op">=</span><span class="bu">memoryview</span>(<span class="bu">bytes</span>(<span class="dv">12</span>))<span class="co"># memory view bytes array data type</span></span> <span id="cb57-13"><a href="#cb57-13" aria-hidden="true" tabindex="-1"></a>m<span class="op">=</span><span class="st">b'245'</span><span class="co"># byte data type</span></span> <span id="cb57-14"><a href="#cb57-14" aria-hidden="true" tabindex="-1"></a><span class="co">#the prinbt function below is to output the results of the data types</span></span> <span id="cb57-15"><a href="#cb57-15" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(a)</span> <span id="cb57-16"><a href="#cb57-16" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(b)</span> <span id="cb57-17"><a href="#cb57-17" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(c)</span> <span id="cb57-18"><a href="#cb57-18" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(d)</span> <span id="cb57-19"><a href="#cb57-19" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(e)</span> <span id="cb57-20"><a href="#cb57-20" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(f)</span> <span id="cb57-21"><a href="#cb57-21" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(g)</span> <span id="cb57-22"><a href="#cb57-22" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(h)</span> <span id="cb57-23"><a href="#cb57-23" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(i)</span> <span id="cb57-24"><a href="#cb57-24" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(j)</span> <span id="cb57-25"><a href="#cb57-25" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(k)</span> <span id="cb57-26"><a href="#cb57-26" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(l)</span> <span id="cb57-27"><a href="#cb57-27" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(m)</span> <span id="cb57-28"><a href="#cb57-28" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="bu">type</span>(a), <span class="bu">type</span>(b), <span class="bu">type</span>(c), <span class="bu">type</span>(d), <span class="bu">type</span>(e), <span class="bu">type</span>(f), <span class="bu">type</span>(g), <span class="bu">type</span>(h), <span class="bu">type</span>(i), <span class="bu">type</span>(j), <span class="bu">type</span>(k), <span class="bu">type</span>(l), <span class="bu">type</span>(m))</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
string 12 12.9 12j [1, 2, 3, 4, 5, 66.77, 78j, 'string'] (1, 2, 344.56, 45j, 'string') frozenset({344.56, 1, 2, 'string'}) {'one': 1.1, 'two': 2j, 'three': 'three', 'four': [4, 4, 4]} {1, 2, 3, 4, 5, 6, 7, 8, 9} True bytearray(b'\x00\x00') <memory at 0x7d76740a1c00> b'245' <class 'str'> <class 'int'> <class 'float'> <class 'complex'> <class 'list'> <class 'tuple'> <class 'frozenset'> <class 'dict'> <class 'set'> <class 'bool'> <class 'bytearray'> <class 'memoryview'> <class 'bytes'> |
The above pioece of code shows the examples of different data types in python. the datatypes in python are very crusial as they are the building block of pytrhon programming and must be understood very well for one to have a mastery in python.
1 2 3 4 5 6 7 8 9 |
<span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="dv">12</span><span class="co"># this is an integer datatype belonging in python numbers</span></span> <span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="fl">12.23</span><span class="co">#this is a float data type belonging in python numbers</span></span> <span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a>z<span class="op">=</span><span class="ot">12j</span><span class="co">#this is a complex datatype belonging to pythoin numbers</span></span> <span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a><span class="co">#the above belong to python numbers</span></span> <span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> <span id="cb59-6"><a href="#cb59-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(y)</span> <span id="cb59-7"><a href="#cb59-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(z)</span> <span id="cb59-8"><a href="#cb59-8" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="bu">type</span>(x), <span class="bu">type</span>(y), <span class="bu">type</span>(z))</span> <span id="cb59-9"><a href="#cb59-9" aria-hidden="true" tabindex="-1"></a><span class="co">#the expected output should display a list of data types for each</span></span> |
1 2 3 4 5 |
12 12.23 12j <class 'int'> <class 'float'> <class 'complex'> |
also note they can be negatives also below is an example to illustrate that
1 2 3 4 5 6 |
<span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=-</span><span class="dv">20</span></span> <span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=-</span><span class="fl">20.23</span></span> <span id="cb61-3"><a href="#cb61-3" aria-hidden="true" tabindex="-1"></a>z<span class="op">=-</span><span class="ot">12.78j</span></span> <span id="cb61-4"><a href="#cb61-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> <span id="cb61-5"><a href="#cb61-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(y)</span> <span id="cb61-6"><a href="#cb61-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(z)</span> |
1 2 3 4 |
-20 -20.23 (-0-12.78j) |
we shall discuss about type conversion in the subsequent cells, sometimes you will need to convert a certain data type from one Form to another then use the converted datatype
1 2 3 4 5 6 7 |
<span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="bu">int</span>(<span class="fl">23.67</span>)<span class="co">#converting float to integer</span></span> <span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="bu">float</span>(<span class="dv">45</span>)<span class="co">#convering integer to float</span></span> <span id="cb63-3"><a href="#cb63-3" aria-hidden="true" tabindex="-1"></a>z<span class="op">=</span><span class="bu">str</span>(<span class="dv">23</span>)<span class="co">#converting integer to string</span></span> <span id="cb63-4"><a href="#cb63-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> <span id="cb63-5"><a href="#cb63-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(y)</span> <span id="cb63-6"><a href="#cb63-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(z)</span> <span id="cb63-7"><a href="#cb63-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="bu">type</span>(x), <span class="bu">type</span>(y), <span class="bu">type</span>(z))</span> |
1 2 3 4 5 |
23 45.0 23 <class 'int'> <class 'float'> <class 'str'> |
In the above code we see that the type casting has been used
1 2 |
<span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random<span class="co">#this is a python module to randomly generate numbers</span></span> <span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(random.randrange(<span class="dv">1</span>,<span class="dv">20</span>,<span class="dv">2</span>))<span class="co">#this is used to output the random numbers start number 1, stop number 20 and step size number 2</span></span> |
1 2 |
5 |
below is an example of the random numbers
1 2 3 |
<span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span> random.randint(<span class="dv">1</span>,<span class="dv">10</span>)</span> <span id="cb67-3"><a href="#cb67-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> |
1 2 |
8 |
1 2 3 |
<span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>random.randrange(<span class="dv">1</span>,<span class="dv">10</span>)</span> <span id="cb69-3"><a href="#cb69-3" aria-hidden="true" tabindex="-1"></a>x</span> |
1 |
3 |
1 2 3 4 |
<span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb71-2"><a href="#cb71-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>,<span class="dv">10</span>]</span> <span id="cb71-3"><a href="#cb71-3" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>random.shuffle(x)</span> <span id="cb71-4"><a href="#cb71-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> |
1 2 |
None |
1 2 3 4 |
<span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="bu">list</span>((<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>,<span class="dv">10</span>))</span> <span id="cb73-3"><a href="#cb73-3" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>random.choice(x)</span> <span id="cb73-4"><a href="#cb73-4" aria-hidden="true" tabindex="-1"></a>x</span> |
1 |
5 |
1 2 3 |
<span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>random.random()</span> <span id="cb75-3"><a href="#cb75-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(x)</span> |
1 2 |
0.3786340893997494 |
1 2 3 4 |
<span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="bu">list</span>((<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>,<span class="dv">10</span>))</span> <span id="cb77-3"><a href="#cb77-3" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>random.sample(x, <span class="dv">7</span>)</span> <span id="cb77-4"><a href="#cb77-4" aria-hidden="true" tabindex="-1"></a>x</span> |
1 |
[8, 1, 4, 3, 10, 5, 6] |
1 2 3 4 |
<span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb79-2"><a href="#cb79-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span><span class="bu">list</span>((<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>,<span class="dv">10</span>))</span> <span id="cb79-3"><a href="#cb79-3" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>[random.choice(x) <span class="cf">for</span> _ <span class="kw">in</span> <span class="bu">range</span> (<span class="dv">3</span>)]</span> <span id="cb79-4"><a href="#cb79-4" aria-hidden="true" tabindex="-1"></a>x</span> |
1 |
[10, 3, 1] |
The above statements or code show how youcan use the random to generate number data type
1 2 |
<span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">'Hello World'</span>)</span> <span id="cb81-2"><a href="#cb81-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Hello world"</span>)</span> |
1 2 3 |
Hello World Hello world |
The above lines of code show how one can implement the use of strings in python. Note that hello world is a string.
So what is a string in python? A string in python is surrounded by quotation marks ether single quotation mark or Double Quotation mark
1 2 3 4 5 6 7 8 9 |
<span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span> <span id="cb83-2"><a href="#cb83-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span> <span id="cb83-3"><a href="#cb83-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> random</span> <span id="cb83-4"><a href="#cb83-4" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>[random.randint(<span class="dv">1</span>,<span class="dv">100</span>) <span class="cf">for</span> _ <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">5</span>)]</span> <span id="cb83-5"><a href="#cb83-5" aria-hidden="true" tabindex="-1"></a>x<span class="op">=</span>pd.DataFrame(x, index<span class="op">=</span>[<span class="st">'a'</span>,<span class="st">'b'</span>,<span class="st">'c'</span>,<span class="st">'d'</span>,<span class="st">'e'</span>])</span> <span id="cb83-6"><a href="#cb83-6" aria-hidden="true" tabindex="-1"></a>y<span class="op">=</span><span class="st">'This is a string enclosed with a single comment'</span></span> <span id="cb83-7"><a href="#cb83-7" aria-hidden="true" tabindex="-1"></a>z<span class="op">=</span><span class="st">"This is a string enclosed in a double quote"</span></span> <span id="cb83-8"><a href="#cb83-8" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(y, <span class="st">'</span><span class="ch">\n</span><span class="st">'</span>, z)</span> <span id="cb83-9"><a href="#cb83-9" aria-hidden="true" tabindex="-1"></a>x</span> |
1 2 3 |
This is a string enclosed with a single comment This is a string enclosed in a double quote |
0 | |
---|---|
a | 89 |
b | 22 |
c | 52 |
d | 78 |
e | 2 |