Introduction
Python is a high-level programming language with dynamic binding and high-level inherent data structures. It's an object-oriented programming language with an interpreted syntax. Python stands out from other programming languages because of its simple to write and comprehend syntax, which appeals to both novices and experts. Python's broad applicability and library support enable developers to create extremely flexible and scalable software and products in the real world.
The Zen of Python: Tim Peters, a long-time Python programmer, condenses the BDFL's guiding principles for Python's design into 20 aphorisms, of which only 19 have been written down. Peters's Zen of Python was included as entry number 20 in the language's official Python Enhancement Proposals and was released into the public domain.[3] It is also included as an Easter egg in the Python interpreter, where it can be displayed by entering importthis. In the May of 2020, Barry Warsaw wrote the lyrics to the music.
We will start with the basics of python, on this cheatsheet.
The Arithmetic Operators in the below table are in Lowest to Highest precedence.
The preceding operations are coded in interactive shell as follows:
Data Types
Examples
Variables: Variables are names for data elements that can take on one or more values throughout the execution of a program. Variables come into existence when you assign
a value to them.
Rules for naming a variable:
Example:
but,
hence, spam should not be used in the code for a second time.
Comments: Comments are lines of text or code in a program that the compiler ignores during execution. They are used to explain a certain line of code to the viewer. Different types of comments in python are as follows :
:
Basic Functions:
type casting can be defined as a conversion of variables of a particular datatype into some other datatype, with the guarantee that the conversion is valid.
Type casting can be of two types:
Implicit Type Casting: Implicit type casting occurs when the Python compiler internally typecasts one variable into another type without the user's intervention.
Explicit Type Casting: When the user uses explicit type casting, the compiler is forced to transform a variable from one type to another. Different types of typecasting are :
The following Table gives a list of relational operators in Python along with their functions:
8. Boolean Operators in Python: boolean operations available in python are as follows:
Loop Statements in Python : In Python, loops are statements that allow us to repeat an operation unless a given condition is met.
For Loops: The for loop is used to iterate across iterables like as strings, tuples, lists, and so on and it is used to repeat a set of statements or a section of a program multiple times
While Loops:
This iterates over all of the items in a Python container, such as a list, tuple, dictionary, and so on.
Break : break statements are used to exit the current loop and allow the following statement to be executed.
Continue: the continue statement allows us to return control to the loop's beginning while skipping all lines of code below it.
Functions are used to structure and improve the readability and reusability of our code. The def keyword is used to define a function in Python. Depending on the use case, a function can return a value or not. The return statement (which has been discussed) is utilized if it has to return a value.
Example:
Global Statement: To modify a global variable from inside a function, we use the global statement.
The value of "value" has been set to Global. We use the global keyword in conjunction with "value" to change its value to local and then print it from within the function.
Importing Modules in Python: Python contains a number of third-party libraries providing helpful tools and functions. To use these modules, we must use the import keyword to import them into our code. For example, if we wish to use the math module's features, we may import it using import math in our Python code, as seen in the example below.
If we need to perform any string manipulations, we can use the string module as the import string in python. More on this in the String Manipulation section below.
Exception Handling in Python: Exception Handling is used to handle situations in our program flow, which can inevitably crash our program and hamper its normal working. It is done in Python using try-except-finally keywords.
Lists in Python: Lists are used to store multiple items in a single variable. Their usage and some functions are shown below with examples:
Accessing elements in a List: Getting the value of an element at any arbitrary index in a list is called accessing elements in a list. In Python, indexes are assigned on a 0 basis. In Python, we may also use negative indexes to retrieve elements. Negative indexes denote elements that are counted from the list's back (end).
Slicing a List: Slicing is the process of accessing a part or subset of a given list. The slicing is explained in the image below:
Changing Values in a List: We can modify the value of an element in a list by accessing it with [] and then setting it to a different value.
List Concatenation and Replication: Concatenation is the process of combining the contents of two lists into one.
Replication: List replication is the process of copying the contents of a list a finite number of times into the same or another list.
Delete values from Lists: Using the del keyword, we can remove a specific element from a list.
Looping through Lists: The below example shows how we can iterate over all the elements present in a list.
in and not in keywords: We can use the in keyword to see if a specific element is present in a Python variable.We can check if a specific element is not present in the specified Python variable, similarly to the not in keyword.
Adding Values in Lists:
.
Sorting a List: Sorting a list entails organising the list's contents in a specific order. The sort() method is used to sort a list.
The members of the tuple are initialised by writing them with (, separated by commas).
Type Converting between Tuples, Lists, and Strings:
Output:
Update key value in dictionary:
Some other functions :
join() and split() Functions: The join() function joins elements of a list with a delimiter string and returns a string as the result.
split() function splits the into tokens, based on some delimiters and returns the result as a list:
String Formatting: String Formatting is done with the str.format() function.
Template Strings: When formatting strings provided by users, it is advised that you use it. They simplify the code and make it easier to grasp. Importing the Template class from the string module allows you to use them.
Date and Time: To handle date and time operations in Python, we use the datetime module.
Raising Exceptions with raise statement: The raise statement is used to raise exceptions and consists of 3 components:
Traceback as String
In Python, there is a method called traceback. When a raised Exception is not treated as a String type, format exc() produces the traceback presented by Python. This will necessitate the import of the Traceback Module.
Assert Statements in Python: Assert Statements/Assertions are commonly used for debugging programs and determining whether the code is executing an operation that is clearly incorrect based on the program's logic. The unique feature of assert is that when it fails, the program crashes quickly, letting us to focus our search for the fault.
The following elements are included in the creation of an assert statement:
Logging enables us to keep track of certain events that occur when software is run. It plays a critical role in software development, debugging, and running.
considering the function which multiplies 2 numbers:
In equivalent the lamba function will be:
Ternary Operator in Python: The ternary operator is a one-liner statement that can be written as an alternative to if-else conditionals.
WHAT IS WEB3?Web3 is the catch-all term for the vision of an upgraded version of the internet aimed at giving power back to the users. It is truly the......
Operating Systems is one of the core subjects for a computer science student. As such a lot of important questions are asked on this subject in interv......
Getting started with cloud computing ?The words "cloud" and "web" will be interchangeable. What kind of business operates today without the internet? ...
SOFTWARE MAINTENANCE:Software Maintenance can be defined as the process of modifying a software product after its delivery. The main purpose of softwa...
A quick summary upto what we did this far from this series:Software EngineeringSoftware engineering is a discipline of engineering concerned wit...
Introduction:Understanding time complexity of a function is imperative for every programmer to execute a program in the most efficient manner. Time co...
Software MethodologyA software methodology (which is also known as software process) is a set of related development Phases that leads to the pr...
Software Engineering is a subject that requires when you are a software developer as well as when you are in your college preparing for a semester or ...
Are you getting ready for your SQL developer job interview?You've come to the correct place.This tutorial will assist you in brushing up on your SQL s...
Introduction: What is SQL?To understand SQL, we must first understand databases and database management systems (DBMS).Data is essentially a collectio...
Networking: Importance in real life!Using a computer for work, as well as for personal use, ha...
FORMULA LIST:ALGEBRA :1.Sum of first n natural numbers = n(n+1)/22.Sum of the squares of first n nat...
ER-DiagramWhat are ER Models ?An Entity Relationship Diagram (ERD) is a visual representation of dif...
What is actually DBMS?A Database Management System (DBMS) is system software used to manage the orga...
C CHEATSHEETINTRODUCTION:C programming language was developed by Dennis Ritchie of Bell Labs in 1972...
C is a general purpose high-level language most popular amongst coders, it is the most compatible, e...
C++ programming language has templates which basically allows functions and classes to work with gen...
C++ was conceived in the late 1970s to overcome the limitations of C. It is an extension of th...
Interview Questions are a great source to test your knowledge about any specific field. But remember...
The most popular high-level, multipurpose programming language right now is Python.Python supports p...
Interview Questions are a great source to test your knowledge about any specific field. But remember...
Object-Oriented Programming or better known as OOPs is one of the major pillars of Java that has lev...
Java is a high-level programming language known for its robustness, object-oriented nature, enhanced...