Basic syntax from the python programming language
The print function is used to display or print output as follows
The input function is used to take input as string or character from the user as follows:
To take input in form of other datatypes we need to typecaste them as follows:-
To take input as an integer:-
To take input as an float:-
range function returns a sequence of numbers, eg, numbers starting from 0 to n-1 for range(0, n)
Here the start value and step value are by default 1 if not mentioned by the programmer. but int_stop_value is the compulsory parameter in range function
example-
Comments are used to make the code more understandable for programmers, and they are not executed by compiler or interpreter.
An escape sequence is a sequence of characters; it doesn't represent itself (but is translated into another character) when used inside a string literal or character. Some of the escape sequence characters are as follows:
Newline Character
It adds a backslash
It adds a single quotation mark
It gives a tab space
It adds a backspace
It represents the value of an octal number
It represents the value of a hex number
Carriage return or \r will just work as you have shifted your cursor to the beginning of the string or line.
Python string is a sequence of characters, and each character can be individually accessed using its index.
You can create Strings by enclosing text in both forms of quotes - single quotes or double quotes.
Example:
The position of every character placed in the string starts from the 0th position and steps by step, it ends at the length-1 position.
Slicing refers to obtaining a sub-string from the given string. The following code will include index 1, 2, 3, and 4 for the variable named var_name.
Slicing of the string can be obtained by the following syntax:
Here start and step value are considered 0 and 1, respectively if not mentioned by the programmer.
Returns True if all the characters in the string are alphanumeric, else False.
Returns True if all the characters in the string are alphabets.
Returns True if all the characters in the string are decimals.
Returns True if all the characters in the string are digits.
Returns True if all characters in the string are lowercase.
Returns True if all characters in the string are whitespaces.
Returns True if all characters in the string are uppercase.
Converts a string into lowercase equivalent.
Converts a string into uppercase equivalent.
It removes leading and trailing spaces in the string.
A List in Python represents a list of comma-separated values of any data type between square brackets.
These elements can be of different data types.
The position of every element placed in the list starts from the 0th position and step by step, it ends at length-1 position.
List is ordered, indexed, mutable, and the most flexible and dynamic collection of elements in Python.
This method allows you to create an empty list.
Returns the index of the first element with the specified value.
Adds an element at the end of the list.
Add the elements of a given list (or any iterable) to the end of the current list.
Adds an element at the specified position.
Removes the element at the specified position and returns it.
The remove() method removes the first occurrence of a given item from the list.
Removes all the elements from the list.
Returns the number of elements with the specified value.
Reverses the order of the list.
Sorts the list.
Tuples are represented as comma-separated values of any data type within parentheses.
These elements can be of different data types.
The position of every element placed in the tuple starts from the 0th position and step by step, it ends at length-1 position.
Tuples are ordered, indexed, immutable, and the most secured collection of elements.
Lets talk about some of the tuple methods:
It returns the number of times a specified value occurs in a tuple.
It searches the tuple for a specified value and returns the position.
A set is a collection of multiple values which is both unordered and unindexed. It is written in curly brackets.
Set is an unordered, immutable, non-indexed type of collection. Duplicate elements are not allowed in sets.
Set Methods
Adds an element to a set.
Remove all elements from a set.
Removes the specified item from the set.
Returns intersection of two or more sets.
Checks if a set is a subset of another set.
Removes an element from the set.
Removes the specified element from the set.
Returns the union of two or more sets.
The dictionary is an unordered set of comma-separated key:value pairs, within {}, with the requirement that within a dictionary, no two keys can be the same.
Dictionary is an ordered and mutable collection of elements. Dictionary allows duplicate values but not duplicate keys.
By putting two curly braces, you can create a blank dictionary.
By this method, one can add new elements to the dictionary.
If a specified key already exists, then its value will get updated.
The `del` keyword is used to delete a specified key:value pair from the dictionary as follows:
Below are some of the methods of dictionaries.
It returns the length of the dictionary, i.e., the count of elements (key: value pairs) in the dictionary.
Removes all the elements from the dictionary.
Returns the value of the specified key.
Returns a list containing a tuple for each key-value pair.
Returns a list containing the dictionary's keys.
Returns a list of all the values in the dictionary.
Updates the dictionary with the specified key-value pairs.
In Python, indentation means the code is written with some spaces or tabs into many different blocks of code to indent it so that the interpreter can easily execute the Python code.
Indentation is applied on conditional statements and loop control statements. Indent specifies the block of code that is to be executed depending on the conditions.
The if, elif, and else statements are the conditional statements in Python, and these implement selection constructs (decision constructs).
Example:
A loop or iteration statement repeatedly executes a statement, known as the loop body, until the controlling expression is false (0).
Example:
Example:
The break statement enables a program to skip over a part of the code. A break statement terminates the very loop it lies within.
Example:
The continue statement skips the rest of the loop statements and causes the next iteration to occur.
Example:
A function is a block of code that performs a specific task. You can pass parameters into a function. It helps us to make our code more organized and manageable.
The `def` keyword is used before defining the function.
Whenever we need that block of code in our program, simply call that function name whenever needed. If parameters are passed during defining the function, we have to pass the parameters while calling that function.
Example:
The function return statement returns the specified value or data item to the caller.
Arguments are the values passed inside the parenthesis of the function while defining as well as while calling.
Example:
File handling refers to reading or writing data from files. Python provides some functions that allow us to manipulate data in the files.
r - to read the content from the file
w - to write the content into the file
a - to append the existing content into the file
r+ - To read and write data into the file. The previous data in the file will be overridden.
w+ - To write and read data. It will override existing data.
a+ - To append and read data from the file. It won’t override existing data.
The read function contains different methods: read(), readline(), and readlines()
This function writes a sequence of strings to the file.
An exception is an unusual condition that results in an interruption in the flow of a program.
A basic try-catch block in Python. When the try block throws an error, the control goes to the except block.
The else block is executed if the try block has not raised any exception, and the code has been running successfully.
The finally block will be executed even if the try block of code has been running successfully or the except block of code is executed. The finally block of code will be executed compulsorily.
It is a programming approach that primarily focuses on using objects and classes. The objects can be any real-world entities.
The syntax for writing a class in Python:
Instantiating an object can be done as follows:
The self parameter is the first parameter of any function present in the class. It can be of a different name, but this parameter is a must while defining any function in the class, as it is used to access other data members of the class.
Constructor is a special function of the class used to initialize the objects. The syntax for writing a class with a constructor in Python:
By using inheritance, we can create a class which uses all the properties and behavior of another class. The new class is known as a derived class or child class, and the one whose properties are acquired is known as a base class or parent class.
It provides the re-usability of the code.
The filter function allows you to process an iterable and extract those items that satisfy a given condition.
Used to find whether a class is a subclass of a given class or not, as follows:
Here are some of the advanced topics of the Python programming language like iterators and generators.
Used to create an iterator over an iterable.
Used to generate values on the fly.
Decorators are used to modifying the behavior of a function or a class. They are usually called before the definition of a function you want to decorate.
It is used to set the property 'name'
It is used to delete the property 'name'