Analytics Vidhya
Published in

Analytics Vidhya

Learn Python -20 Basic Concepts

A to Z Basics of Python - fast track to get started

source:https://www.istockphoto.com/photo/abstract-motion-curvy-urban-road-gm1181339364-331291666

If you want to quickly start working on python by learning end to end of basics, you are at the right place. I am using the Jupyter notebook for the purpose of this exercise. We can get the Jupyter notebook by downloading and installing anaconda from the link, which is a open-source distribution of Python. If you are new to Jupyter notebook please go through this nice blog which explains how to launch it and use. With that being said lets start the work.

Below is he list of 20 topics we quickly go through.

1. Core Data types: Data types shows what type of value the variable holds. In Python we have the 4 basic data types as below.

a. Int — Any Whole number without a decimal point.

b. Float — Any number with a decimal point.

c. String — Surrounded by single or double quotes.

d. Bool — Boolean Values, True or False

2. Print : The print() function sends the required message to the screen, or to output device. The message can be a string, or any other object and it will be converted into a string before printing. Multiple values can be printed by putting a comma as a separator.

3. Variables : Variables are basically to store data values in the memory. We can create and assign the values to variables on the fly. To create a variable we just need a to assign a value to it. Variable names cannot start with a number and special character is not allowed except underscore.

4. User Input: The input() function allows user input. It takes the line entered on a console and convert it into a string & returns it.

5. Arithmetic Operators: In Python also we have basic arithmetic operators like Addition, Subtraction, Multiplication , Division and Exponential. By default the order of precedence is BEDMAS (Brackets, Exponential, Division, Multiplication, Addition, Subtraction). Brackets need be used to override the precedence.

6. String Methods: Methods are dot(.) operators used to perform some operation on the string on which we apply. Below are few sample string methods.

7. Conditional Operators: These operators compare two values or variables and says if it is True or False. We have different operators like Equal to (==), Not Equal to (!=), Less/greater than or Equal to (≤, ≥) and less/greater than (<, >). There are few others but these are the core conditional operators.

8. If/Else/Elif: These statements are used in Python for decision making. It evaluates the expression and execute the statements if it is True.

9. Collections: A collection is an unordered or ordered group of elements. In Python we have two types of collections, List and Tuple.

List: List is a collection of elements in an ordered manner, similar to an array. The index position of first element in the list starts with zero. Lists are mutable, we can add or remove the elements to and from the list.

Tuple: Tuple is similar to List except that they are immutable. If we need to change it, we need to redefine it. Tuple uses round brackets in contrast to List which uses square brackets.

10. For & While Loops: There are 2 types of loops in Python. For loop is used for iterating over list, a tuple, a dictionary, a set, or a string. We can specify start, stop and step interval during looping.

While : The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. We generally use this loop when we don’t know the number of times to iterate beforehand.

11. Slice Operator: It allows us to take a slice of a string, list or tuple. Slice operator contains a set of square brackets with a sequence of colons and numbers within it.

sliced = [start:strop:step]

12. Set: A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Usually used for lookups. Set operations are tend to be much faster compare to List.

13. Dictionary: Dictionary in Python is an unordered collection of data values, used to store data values like a map. These are essentially a key value pairs.

a = {key:value}

14. Comprehensions : Comprehensions are not found in other programming languages. It provides us with a short and concise way to construct new sequences (such as lists, set, dictionary etc.).

15. Function: Functions are a convenient way to divide our code into useful blocks, allowing us to order our code, make it more readable and reuse it. Function code executes only when it is referred.

16. Unpack Operators (*args & **kwargs): Unpack operators separates a collection of elements from a list or tuple into individual elements. *args for positional and **kwargs for keyward arguments. Usually used to unpack and pass as arguments to a function. Single asterisk (*) is for Lists/Tuples and double asterisk (**) is for dictionaries.

17. Raising and Handling Exceptions: In Python we can choose to throw an exception if a condition occurs. We use the key word raise to do this and we can pass the required description within brackets.

Handling Exceptions: In Python, ‘try’ statement is used to handle exceptions .The operation which can raise an exception is placed inside within the ‘try’ clause. The code that handles the exceptions is placed in the ‘except’ clause.

We have something called ‘finally’, the codes block mentioned under this clause will be executed no matter if the try block raises an error or not. This can be useful to close objects and clean up resources.

18. Lambda: While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Lambda function can take any number of arguments, but can only have one expression.

19. Map and Filter: The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns a list of the results.

Filter: The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

20. F-Strings: One of the cool way to format strings. F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time.

The notebook used is present here.

Thanks for your time and hope this give you some basic idea about python.

Resources Referred: Most of the definitions are taken from the below sources with thanks.

a. https://www.programiz.com/python-programming/

b. https://www.w3schools.com/python

c. https://www.learnpython.org/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store