Python Ebook Final.pdf
Document Details
Uploaded by Deleted User
Full Transcript
Python EBook By @Curious_.programmer Copyrighted By @Curious_.programmer 1.1 What Is Python? Python is a programming language that is commonly used to create websites and applications, automate processes, and do data analysis. Python is a general- purpose programming language, w...
Python EBook By @Curious_.programmer Copyrighted By @Curious_.programmer 1.1 What Is Python? Python is a programming language that is commonly used to create websites and applications, automate processes, and do data analysis. Python is a general- purpose programming language, which means it can be used to construct a wide range of applications and isn't specialized for any particular problem. Because of its flexibility and beginner-friendliness, it has become one of the most widely used programming languages today. Python is a general purpose, dynamic, high-level, and interpreted programming language. It supports Object Oriented programming approach to develop applications. It is simple and easy to learn and provides lots of high- level data structures. Python is easy to learn yet powerful and versatile scripting language, which makes it attractive for Application Development. Python's syntax and dynamic typing with its interpreted nature make it an ideal language for scripting and rapid application development. 1.2 Why Python Is Popular? Python's popularity is due to a variety of factors. Here's a closer look at what makes it so useful to programmers It features a straightforward syntax that resembles normal English, making it easy to read and comprehend. This speeds up the development of projects as well as the improvement of existing ones. It's adaptable. Python may be used for a variety of purposes, including web development and machine learning. It's user-friendly, making it popular among new programmers. It's free to use and distribute, even for commercial, because it's open source. The Python module and library archive—bundles of code produced by third-party users to extend Python's capabilities— is large and expanding. Python has a strong community that adds to the library of modules and libraries and serves as a resource for other programmers. Because of the large support network, finding a solution to a stumbling block is quite simple; someone has almost certainly encountered the same issue before. 1.3 What can be done using Python? Python is widely used for web and software development, task automation, data analysis, and data visualization. Python has been embraced by many non- programmers, such as accountants and scientists, for a range of common activities, such as arranging money, due to its relative ease of learning. Data analysis and machine Learning Python has become a data science standard, allowing data analysts and other professionals to do complicated statistical computations, generate data visualization’s, construct machine learning algorithms, manage and analyses data, and perform other data-related activities using the language. Web development Python is frequently used to create a website's or application's back end—the bits that the user does not see. Sending data to and from servers, processing data and connecting with databases, URL routing, and maintaining security are all things that Python can help with in web development. For web development, Python has a number of frameworks. Django and Flask are two popular frameworks Automation or scripting If you find yourself repeating a job, you may make it more efficient by automating it with Python. Scripting is the process of writing code to create these automated operations. Automation may be used in the coding industry to check for mistakes across many files, convert files, do simple math, and eliminate duplicates in data. 1.4 Why learn Python? Python provides many useful features to the programmer. These features make it most popular and widely used language. We have listed below few-essential feature of Python. Interpreted Language Object-Oriented Language Open Source Language Extensible Learn Standard Library GUI Programming Support Integrated Embeddable Dynamic Memory Allocation 1.5 Python Installation 1.5.1 Downloading installing Python 1. Go to www.python.org/downloads/ 2. Download Python as per your system requirement Installing python on windows 1. Click on Python Releases for Windows, select the link for the latest Python3 Release – Python 3.x.x 2. Scroll to the bottom and select either Windows x86-64 executable Installer for 64-bit or Windows x86 executable installer for 32-bit Installing python on Linux 1. Open the Ubuntu Software Center folder 2. Select Developer Tools from the All Software drop-down list box 3. Double-click the Python 3.3.4 entry 4. Click Install 5. Close the Ubuntu Software Center folder 1.6 Python First Program Unlike the other programming languages, Python provides the facility to execute the code using few lines. We can do this using one statement in Python. 2.1 Modules in Python A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized. 2.2 Comments in Python Python Comment is an essential tool for the programmers. Comments are generally used to explain the code. We can easily understand the code if it has a proper explanation. A good programmer must use the comments because in the future anyone wants to modify the code as well as implement the new module; then, it can be done easily. Single line comment Python single line comment starts with the hashtag symbol (#) with no white spaces and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment Example: Multiline Comments Python does not provide the option for multiline comments. However, there are different ways through which we can write multiline comments. Python ignores the string literals that are not assigned to a variable so we can use these string literals as a comment. Example: 2.3 What is pip? pip is a package-management system written in Python used to install and manage software packages. It connects to an online repository of public packages, called the Python Package Index. pip can also be configured to connect to other package repositories, provided that they comply to Python Enhancement Proposal 503. 3.1 Variables in python Variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value. In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type. Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore. Example: 3.2 Identifier in python Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. Rules: 1. The first character of the variable must be an alphabet or underscore ( _ ). 2. All the characters except the first character may be an alphabet of lower-case (a-z), upper-case (A-Z), underscore, or digit (0-9). 3. Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *). 4. Identifier name must not be similar to any keyword defined in the language. 5. Examples of valid identifiers: a123, _n, n_9, etc. 6. Examples of invalid identifiers: 1a, n%4, n 9, etc. 3.3 Data Types in python Variables can hold values, and every value has a data- type. Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type. a=5 The variable a holds integer value five and we did not define its type. Python interpreter will automatically interpret variables a as an integer type. Python enables us to check the type of the variable used in the program. Python provides us the type() function, which returns the type of the variable passed. Python Data Types: 1) Numbers: Number stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type. a) Python supports three types of numeric data- i) Integer - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc. ii) Float- Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. iii) Complex number- A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts, respectively 2) Sequence Type: i) String: The string can be defined as the sequence of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string. ii) List: Python Lists are similar to arrays in C. However, the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets []. iii) Tuple: A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses (). 3) Boolean: Boolean type provides two built-in values, True and False. These values are used to determine the given statement true or false. It denotes by the class bool. True can be represented by any non-zero value or 'T' whereas false can be represented by the 0 or 'F' 4) Set: Python Set is the unordered collection of the data type. It is inerrable, mutable (can modify after creation), and has unique elements. In set, the order of the elements is undefined; it may return the changed sequence of the element. 5) Dictionary: Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type, whereas value is an arbitrary Python object. 3.4 operator in Python The operator can be defined as a symbol which is responsible for a particular operation between two operands. Python provides variety of operators, which are described as follows. o Arithmetic operators o Comparison operators o Assignment Operators o Logical Operators o Bitwise Operators o Membership Operators o Identity Operators Arithmetic operators Arithmetic operators are used with numeric values to perform common mathematical operations Operator Name Example + Addition x+y - Subtraction x-y * Multiplication x*y / Division x/y % Modulus x%y ** Exponentiation x ** y // Floor division x // y Python Assignment Operators: Assignment operators are used to assign values to variables: Operator Example Same As = x=5 x=5 += x += 3 x=x+3 -= x -= 3 x=x-3 *= x *= 3 x=x*3 /= x /= 3 x=x/3 %= x %= 3 x=x%3 //= x //= 3 x = x // 3 **= x **= 3 x = x ** 3 &= x &= 3 x=x&3 |= x |= 3 x=x|3 ^= x ^= 3 x=x^3 >>= x >>= 3 x = x >> 3