




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python programming introduction and description and about the process
Typology: Assignments
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Introduction: History of Python- Python features- Applications-Programming Using the REPL-Running Python Scripts-Variables – Assignment- Keywords- Input-Output- Indentation. Data Types: Single-Value data types - int, float, Complex and Boolean. Multi-Valued Data types - Lists, Tuples, Sets, Dictionaries, Strings- indexing and slicing.
Python is a high-level, general-purpose and a very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting edge technology in Software Industry.
It was initially designed by Guido van Rossum in 1991.
Python was conceptualized in the late 1980s. Guido van Rossum worked that time in a project at the Centrum Wiskunde & Informatica CWI which is situated in Netherlan d.
It was started firstly as a hobby project because he was looking for an interesting project to keep him occupied during Christmas.
The programming language which Python is said to have succeeded is ABC Programming Language, which had the interfacing with the Amoeba Operating System and had the feature of exception handling.
He had already helped to create ABC earlier in his career and he had seen some issues with ABC but liked most of the features.
After that what he did as really very clever. He had taken the syntax of ABC, and some of its good features. It came with a lot of complaints too, so he fixed those issues completely and had created a good scripting language which had removed all the flaws.
The inspiration for the name came from BBC’s TV Show – ‘Monty Python’s Flying Circus’, as he was a big fan of the TV show and also he wanted a short, unique and slightly mysterious name for his invention and hence he named it Python.
The language was finally released in 1991. When it was released, it used a lot fewer codes to express the concepts, when we compare it with Java, C++ & C. Its design philosophy was quite good too. Its main objective is to provide code readability and advanced developer productivity.
Development Steps of Python
Guido Van Rossum published the first version of Python code (version 0.9.0) at alt.sources in February
Python version 1.0 was released in January 1994. The major new features included in this release were the functional programming tools lambda, map, filter and reduce, which Guido Van Rossum never liked.
Six and a half years later in October 2000, Python 2.0 was introduced. This release included list comprehensions, a full garbage collector and it was supporting unicode.
Python flourished for another 8 years in the versions 2.x before the next major release as Python 3. (also known as "Python 3000" and "Py3K") was released.
Page 1
Python is a dynamic, high level, free open source and interpreted programming language. It supports object-oriented programming as well as procedural oriented programming. There are many features in Python.
1. Easy to code: Python is a high-level programming language. Python is very easy to learn the language as compared to other languages like C, C#, Javascript, Java, etc. It is very easy to code in python language and anybody can learn python basics in a few hours or days. It is also a developer-friendly language. 2. Free and Open Source: Python language is freely available at the official website and you can download it from the given download link below click on the Download Python keyword. Since it is open-source, this means that source code is also available to the public. So you can download it as, use it as well as share it. 3. Object-Oriented Language: One of the key features of python is Object-Oriented programming. Python supports object-oriented language and concepts of classes, objects encapsulation, etc. 4. GUI Programming Support: Graphical User interfaces can be made using a module such as PyQt5, PyQt4, wxPython, or Tk in python. PyQt5 is the most popular option for creating graphical apps with Python. 5. High-Level Language: Python is a high-level language. When we write programs in python, we do not need to remember the system architecture, nor do we need to manage the memory. 6. Extensible feature: Python is a Extensible language. We can write us some Python code into C or C++ language and also we can compile that code in C/C++ language. 7. Python is Portable language: Python language is also a portable language. For example, if we have python code for windows and if we want to run this code on other platforms such as Linux, Unix, and Mac then we do not need to change it, we can run this code on any platform. 8. Interpreted Language: Python is an Interpreted Language because Python code is executed line by line at a time. like other languages C, C++, Java, etc. there is no need to compile python code this makes it easier to debug our code. The source code of python is converted into an immediate form called bytecode. 9. Large Standard Library Python has a large standard library which provides a rich set of module and functions so you do not have to write your own code for every single thing. There are many libraries present in python for such as regular expressions, unit-testing, web browsers, etc. 10. Dynamically Typed Language: Python is a dynamically-typed language. That means the type (for example- int, double, long, etc.) for a variable is decided at run time not in advance because of this feature we don’t need to specify the type of variable. Page 2
What is REPL? REPL is the language shell, the Python Interactive Shell. The REPL acronym is short for Read, Eval, Print and Loop. The process is:
> 256* 1024 > Variables can be used in the Python shell too: > width = 10 > height = 20 > size = width*height > print(size) 200 >>>
To start an IDLE interactive shell, search for the IDLE icon in the start menu and double click on it. Page 4
Python IDLE This will open IDLE, where you can write and execute the Python scripts, as shown below. Page 5
Python IDLE To execute a Python script, create a new file by selecting File -> New File from the menu. Page 7
Enter multiple statements and save the file with extension .py using File -> Save. For example, save the following code as hello.py. Python Script in IDLE Now, press F5 to run the script in the editor window. The IDLE shell will show the output. Python Script Execution Result in IDLE Thus, it is easy to write, test and run Python scripts in IDLE. Prepared By G Bhuvaneswari, Asst Professor, CSE Dept, SIETK. Page 8
Orange Orange
Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive. There are 33 keywords in Python 3.7. All the keywords except True, False and None are in lowercase and they must be written as they are. The list of all the keywords is given below. False await Else import pass None break Except in raise True class Finally is return and continue for lambda try as def from nonlocal while assert del global Not with async elif if Or Yield
A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available for Input. input() function This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by python. val = input("Enter your value: ") print(val) Enter your value: 123 When input() function executes program flow will be stopped until the user has given an input. Prepared By G Bhuvaneswari, Asst Professor, CSE Dept, SIETK. Page 10
The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional. Whatever you enter as input, input function convert it into a string. if you enter an integer value still input() function convert it into a string. You need to explicitly convert it into an integer in your code using typecasting. raw_input ( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store.
g = raw_input("Enter your name : ") print g Output Enter your name :sai Sai Here, g is a variable which will get the string value, typed by user during the execution of program. Typing of data for the raw_input() function is terminated by enter key. We can use raw_input() to enter numeric data also. In that case we use typecasting.
print evaluates the expression before printing it on the monitor. Print statement outputs an entire (complete) line and then goes to next line for subsequent output (s). To print more than one item on a single line, comma (,) may be used. Syntax: print (expression/constant/variable) Example 1:
print ('Hello!')
print ("Hello!")
string print ('''Hello!''')
print ('''Hello... how are you? Hey! I am good, what about you? I am good also, thanks.''') Output Hello! Hello! Hello! How are you? Hello... how are you? Prepared By G Bhuvaneswari, Asst Professor, CSE Dept, SIETK. Page 11
Data type defines the type of the variable, whether it is an integer variable, string variable, tuple, dictionary, list etc. Single-Value data types Python Numbers Number data types store numeric values. There are three numeric types in Python:
float complex Variables of numeric types are created when you assign a value to them: Example x = 1 # int y = 2.8 # float z = 1j # complex Integer :Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Example Integers: x = 1 y = 35656222554887711 z = - type(x) type(y) type(z) Output:
Float: Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Example Floats: x = 1.10 y = 1. z = -35. type(x) type(y) type(z) Output:
Prepared By G Bhuvaneswari, Asst Professor, CSE Dept, SIETK. Page 13
Complex Numbers: – Complex number is represented by complex class. It is specified as (real part) + (imaginary part)j. For example – 2+3j Example Complex: x = 3+5j y = 5j z = -5j type(x) type(y) type(z) Output: < class 'complex'>
Booleans : The bool() method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure. Syntax: bool([x]) The bool() method in general takes only one parameter(here x), on which the standard truth testing procedure can be applied. If no parameter is passed, then by default it returns False. So, passing a parameter is optional. It can return one of the two values. It returns True if the parameter or value passed is True. It returns False if the parameter or value passed is False. Here are few cases, in which Python’s bool() method returns false. Except these all other values return True. If a False value is passed. If None is passed. If an empty sequence is passed, such as (), [], ”, etc If Zero is passed in any numeric type, such as 0, 0.0 etc If an empty mapping is passed, such as {}. If Objects of Classes having bool () or len() #check if x and y are equal >>>bool(3==4) False #check if x and y are not equal >>>bool(3!=4) True #check if x is greater than y >>>bool(3>4) False #check if x is less than y >>>bool(3<4) True Prepared By G Bhuvaneswari, Asst Professor, CSE Dept, SIETK. Page 14 method, returning 0 or False
List slices: List slicing is an operation that extracts a subset of elements from an list and packages them as another list. Syntax: Listname[start:stop] Listname[start:stop:steps] default start value is 0 default stop value is n- [:] this will print the entire list [2:2] this will create a empty slice List methods: Methods used in lists are used to manipulate the data quickly. These methods work only on lists. They do not work on the other sequence types that are not mutable, that is, the values they contain cannot be changed, added, or deleted. syntax: list name.method name( element/index/list) Prepared By G Bhuvaneswari, Asst Professor, CSE Dept, SIETK. Page 17
Benefit of Tuple:
If the user wants to protect the data from accidental changes, tuple can be used. Tuples can be used as keys in dictionaries, while lists can't. Operations on Tuples: