CSI-SFIT / Beginners-Guide-to-Python-101

Python 101: Beginners Guide to Python programming. A comprehensive basic guide for a new pythonista

Home Page:https://linktr.ee/CSI_SFIT

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

⚡ Python 101: Beginners Guide to Python Programming📖

BannerImage

A Complete Guide On Python

Table of Contents


Introduction

Python is developed by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It is a widely-used general-purpose, high-level programming language. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.

[Interesting fact: Python is named after the comedy television show Monty Python’s Flying Circus. It is not named after the Python snake.]

Advantages :

  1. Presence of third-party modules
  2. Extensive support libraries(NumPy for numerical calculations, Pandas for data analytics etc)
  3. Open source and community development
  4. Easy to learn
  5. User-friendly data structures
  6. High-level language
  7. Dynamically typed language(No need to mention data type based on value assigned, it takes data type)
  8. Object-oriented language
  9. Portable and Interactive
  10. Portable across Operating systems

Applications :

  1. GUI based desktop applications(Games, Scientific Applications)
  2. Web frameworks and applications
  3. Enterprise and Business applications
  4. Operating Systems
  5. Language Development
  6. Prototyping

Installation

InstallImage

This versatile programming language has two versions: Python 2 and Python 3. Wiki says: Python 2.x is legacy, Python 3.x is the present and future of the language. That is, Python 2 is no longer in development and all new features will be added in Python 3. You can install Python on any operating system such as Windows, Mac OS X, Linux/Unix and others. To install the Python on your operating system, go to this link: https://www.python.org/downloads/. You will see a screen like this.

It is possible to write Python in an Integrated Development Environment, such as Jupiter notebook, Thonny, Pycharm, Netbeans or Eclipse which are particularly useful when managing larger collections of Python files.


Basics

In Python , input() function is used for user’s input and print() for output.

For more details visit


Comments

There are two types of comments in Python.

  1. Single line comment
  2. Multiple line comments

1. Single line comment In python, we use # special character to start the comment.
2. Multi-line comment To have a multi-line comment in Python, we use triple single quotes at the beginning and at the end of the comment.

Comments


Operators

Operators are used to perform operations on variables and values. Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

To learn more visit sites below : (along with problems for practice)


Conditions

If statement An "if statement" is written by using the if keyword.

Elif The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".

Else The else keyword catches anything which isn't caught by the preceding conditions.

Nested If You can have if statements inside if statements, this is called nested if statements.

To learn more visit sites below :

Practice Problems:


Loops

For Loop A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).This is less like the ’for’ keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

While Loop With the while loop we can execute a set of statements as long as a condition is true.

Break Statement With the ‘break’ statement we can stop the loop before it has looped through all the items.

Continue Statement With the continue statement we can stop the current iteration of the loop, and continue with the next.

Range() Function To loop through a set of code a specified number of times, we can use the range() function. The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

To learn more visit sites below :

Practice Problems:


Datatypes

  1. String

    String is a sequence of characters in Python. The data type of String in Python is called “str”. Strings in Python are either enclosed with single quotes or double quotes. Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

To learn more visit sites below :

Practice Problems:

  1. List

Lists are just like dynamic sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it a most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list, which allows duplicating of elements in the list, with each element having its own distinct place and credibility. In Python lists are written with square brackets.

To learn more visit sites below :

Practice Problems:

  1. Tuples

Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily. In Python, tuples are created by placing sequence of values separated by ‘comma’ with or without the use of parentheses for grouping of data sequence.

[Note – Creation of Python tuple without the use of parentheses is known as Tuple Packing.]

To learn more visit sites below :

Practice Problems:

  1. Sets

In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’.

[Note – A set cannot have mutable elements like a list, set or dictionary, as its elements.]

To learn more visit sites below :

Practice Problems:

  1. Dictionary

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key value is provided in the dictionary to make it more optimized.

[Note – Keys in a dictionary doesn’t allow Polymorphism.]

In Python, a Dictionary can be created by placing sequence of elements within curly {}braces, separated by ‘comma’. Dictionary holds a pair of values, one being the Key and the other corresponding pair element being its Key:value. Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be immutable.

[Note – Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly.]

To learn more visit sites below :

Practice Problems:


Functions

A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function. Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are called user-defined functions.

To learn more visit sites below :

Practice Problems:


Classes

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.Class creates a user-defined data structure, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

  • Classes are created by keyword class.
  • Attributes are the variables that belong to class.
  • Attributes are always public and can be accessed using dot (.) operator. Eg.: Myclass.Myattribute

To learn more visit sites below :

Practice Problems:


Regular Expression

Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and shows how regular expressions work in Python. The Python "re" module provides regular expression support.

To understand the RE analogy, MetaCharacters are useful, important and will be used in functions of module re.There are a total of 14 metacharacters as listed below :

RegularExpression

To learn more visit sites below :

Practice Problems:


File Handling

File handling is an important part of any web application.Python has several functions for creating, reading, updating, and deleting files.We use open () function in Python to open a file in read or write mode. As explained above, open ( ) will return a file object. To return a file object we use open() function along with two arguments, that accepts file name and the mode, whether to read or write. So, the syntax being: open(filename, mode).

There are three kinds of mode, that Python provides and how files can be opened:

  • “ r “, for reading.
  • “ w “, for writing.
  • “ a “, for appending.
  • “ r+ “, for both reading and writing

To learn more visit sites below :

Practice Problems:


Resources


Courses


Youtube Channels


Projects Ideas


Practice Problems


Books


How to Contribute

  1. Clone repo and create a new branch: $ git checkout https://github.com/CSI-SFIT/Beginners-guide-to-Python-101.git -b name_for_new_branch.
  2. Make changes and test.
  3. Submit Pull Request with comprehensive description of changes.

Acknowledgements

CSI SFIT Tech Team 2020 - 2021 :

csi_logo