Python basic
Python basic
(The beginning)
Dr. Adarsh Anand
Assistant Professor
Department of Operational Research
University of Delhi, Delhi 110007
adarsh.anand86@gmail.com
What is Python??
Python is an interpreted, object oriented, high level
programming language with dynamic semantics.
Parallel processing can be done in Python but not as elegantly as done in some other languages (like JavaScript
and Go Lang).
• Being an interpreted language, Python is slow as compared to C/C++. Python is not a very good choice for
those developing a high-graphic 3d game that takes up a lot of CPU.
• As compared to other languages, Python is evolving continuously and there is little substantial documentation
available for the language.
• As of now, there are few users of Python as compared to those using C, C++ or Java.
• It lacks true multiprocessor support.
• It has very limited commercial support point.
• Python is slower than C or C++ when it comes to computation heavy tasks and desktop applications.
• It is difficult to pack up a big Python application into a single executable file. This makes it difficult to
distribute Python to non-technical. 5
Application of Python
Network Programming
Data Analysis
Robotics
Games Development
Application of Python
Web Scraping
Data Visualization
Scientific Calculations
Example:
26
Comments
Comments are the non-executable statements in a program. They are just added to describe the
statements in the program code. Comments make the program easily readable and understandable by
the programmer as well as other users who are seeing the code. The interpreter simply ignores the
comments.
In Python, a hash sign (#) that is not inside a string literal begins a comment. All characters following
the # and up to the end of the line are part of the comment
Example:
27
Indentation
Whitespace at the beginning of the line is called indentation . These whitespaces or the indentation are
very important in Python. In a Python program, the leading whitespace including spaces and tabs at the
beginning of the logical line determines the indentation level of that logical line.
Example:
28
Basic Operators in Python
Gates
Binary of a: 0000 1010 (10)
Binary of b: 0000 0100 (4)
Bitwise a & b: 0000 0000 (0)
Bitwise a | b: 0000 1110 (14)
Not operator works only on signed data types conversion is
done using two’s compliment
Right shift : a>> 2 : 0000 0010 (2)
Left shift : a<< 2 : 00 101000 (40)
Operations on Strings
Examples:
39
Slice Operations on Strings
You can extract subsets of strings by using the slice operator ([ ] and [:]). You need to specify index or
the range of index of characters to be extracted. The index of the first character is 0 and the index of the
last character is n-1, where n is the number of characters in the string.
If you want to extract characters starting from the end of the string, then you must specify the index as a
negative number. For example, the index of the last character is -1.
Examples:
40
Lists
Lists are the most versatile data type of Python language. A list consist of items separated by commas
and enclosed within square brackets The values stored in a list are accessed using indexes. The index of
the first element being 0 and n-1 as that of the last element, where n is the total number of elements in
the list. Like strings, you can also use the slice, concatenation and repetition operations on lists.
Examples:
41
Tuples
A tuple is similar to the list as it also consists of a number of values separated by commas and enclosed
within parentheses. The main difference between lists and tuples is that you can change the values in a list
but not in a tuple. This means that while tuple is a read only data type, the list is not.
Examples:
42
Dictionary
Python’s dictionaries stores data in key-value pairs. The key values are usually strings and value can be
of any data type. The key value pairs are enclosed with curly braces ({ }). Each key value pair separated
from the other using a colon (:). To access any value in the dictionary, you just need to specify its key in
square braces ([]).Basically dictionaries are used for fast retrieval of data
Example:
43
Python Type Conversion and Type Casting
• Type Conversion: The process of converting the value of one data type
(integer, string, float, etc.) to another data type is called type conversion.
Python has two types of type conversion.
• Implicit Type Conversion
• Explicit Type Conversion
Implicit Type Conversion:
• In Implicit type conversion, Python automatically converts one data type to
another data type. This process doesn't need any user involvement.