Python PDF chapter =1
GETTING STARTED WITH
PYTHON
INTRODUCTION TO PYTHON
Python Is A General Purpose Interpreted, Interactive, Object-oriented Programming Language.It Was Created By Guido Van Rossum In The Late Eighties And Early Nineties.
FEATURES OF PYTHON
1. It Closely Resembles The English Language. It Is A Programmer Friendly Language.
2. It Is Free And Open Source, i.e. It Is Freely Available ( Can be Downloaded From www.python.org). Open Source Means Its Source Code ( Complete Program Instruction ) Is Also Available. You Can Modify, Improve / Extend An Open Source Software.
3. It Is A High-Level Language Interpreter Based Which Means That The Python Interpreter Interprets And Execute The Code Line By Line. It Also Makes Debugging Easier.
4 . It is an object-oriented programming language.
5 . Python supports modules and packages which encourages program modularity and reuse of code.
6 . It is a platform-independent portable Programming Language i.e It can work on various operating systems. Windows, Linux / UNIX, Macintosh. Smart Phones.
7 . Python can be used in many diverse fields/applications. Some examples of these applications are Web application applications, GUI programming, app development, Game Development, Database application, etc.
8 . It is blessed with a large community.
9 . Python can be used to create desktop and web-based applications.
10 . It has a rich standard library ( https://docs.python.org/3/library ) containing predefined functions for most of the common operations, and a large number of predefined modules ( https://docs.python.org/3/pv-modindex.html ) for specific varieties of functions. This makes programming an easier task.
Minuses of Python Language
1 It is not the fastest language interpreter Since Python is an interpreter-bias-based language. Development time might be fast but the execution time is not as fast compared to other languages.
2. Python has lesser libraries compared to languages like C, Java, and Pearl. At times they have better and multiple solutions than Python
3 . Not strong on type binding It is not strong on catching Type mismatch ' issues. If you declare a variable as an integer and later store a string in it, it will not complain.
4. Because of the lack of syntax Python is an easy language to program in. But difficult to convert a program written in Python to another language, since other Languages have a structure-defined syntax or a strong syntax.
Application Domain
Python is used in many diverse applications. A list of a few application areas where Python is used are :
1. Web and Internet development :
2. Database Access
3 . Desktop GUIS
4. Data Sciences
5. Network Programming
6. Game and 3D Graphics
Python Interface
To develop and execute programs in Python, the Python interpreter is required to be
installed. Python provides an IDLE (Integrated Development and Learning
Environment) for typing, debugging, editing, and executing the program.
Python's IDLE can work in two basic modes: Interactive mode and Script mode.
Interactive mode
When Python IDLE has activated a window entitled "Python Shell will be displayed on the screen. ThisScreen refers to the interactive mode of Python. In this mode, the command, one at a time is entered right of the Python prompt (>>) and Python executes the command there and then and gives the output. This mode is useful for testing out individual statements. The interactive mode does not save the However Command.
To show edit rerun prior command in an interactive window, the up arrow and down arrow keys can be
used. The up arrow key (1 can be used to scroll back card through commands history and the down arrow key (key to scroll forward. Using the Enter key the Command can be executed.
Script mode
In this mode, the python statements are typed in the editor and saved in a .py file. Then the Python interpreter is used to execute the contents from the file. To activate the script mode, follow the given steps:-
•From the python shell window select File ->New File. A window entitled "Untitled" will be opened where the statements can be entered.
•Once the program is ready, first save the file using File->Save as The file will be saved using the .py extension.
•To execute the program select Run ->Run Module (F5 key) The output of the program will be shown in the Python shell window. The file has to be saved before its execution. Otherwise, Python will prompt to Save the file before execution.
Python Character Set
The character set is a set of valid characters that a language can recognize. Python has the following character set:
•Uppercase and Lowercase letters (A-Z, a-z)
•Digits(0-9)
•Special symbols like +/,//**)UL{}<, etc.
•White spaces blank space, tabs, Carriage return, etc...
•Other characters Python can use all the ASCII characters and UNICODE characters as part of data or literal.
Tokens
The smallest individual unit of a python program WHICH CONVEYS SOME MEANING is known as the Tokens or lexical unit. The tokens of the Python program can be categorized into:
keywords, identifiers, literals, operators, and punctuators
Keywords (Reserved Words)
Keywords are the words that convey a special meaning to the Python interpreter. These are reserved for and must not be used as normal identifier names. In python, keywords contain lower case letters only.
Some keywords in python are print, for, if,elif, from, True, None, False, import, in, or, and, not, as, pass, assert, from, break, global, return, class, try, continue, while, def, with, del, special purposes, etc,.
Identifiers
Identifiers are fundamental building blocks of a program and are used to name different parts of a program, i.e. variables, objects, functions, lists, tuples, etc.
√ Rules for naming python identifiers
- An identifier is an arbitrarily long sequence of letters and digits
- The first character must be an alphabet letter or an underscore
- identifier names can contain uppercase alphabets (A Z), lowercase letters (a-z),
digits (0-9) and underscore
- No special characters other than an underscore can be used.
- identifier names are case sensitive and can be unlimited in length
- A keyword or reserved word cannot be used as an identifier
- Names used for object function, List, String, Dictionary, etc. are examples of
Identifiers.
The meaningful name of a variable is called an identifier. variable isles are created the moment you first assign a value to them. To delete a variable the keyword" del is used
Literals
Literals refer to the data items, which do not change their values during program execution (often referred to as constant values). Depending on the data types of the data items, Literals can further be categorized into different categories:-
String literals, Numeric literals, and special literals
String Literal
The text enclosed within quotes forms a string literal in Python. For example,
'a', aa'" a" are all string literals in Python. A string literal is a sequence of characters
within quotes(single, double, or triple).
One can form a string literal by enclosing text in both forms of quotes single or
double. Some valid string literals are:-
Astha "Mehra", 'Hello World',"Raj's", "12345", "1A2B", "a-b-0-d"
Python also allows nongraphic characters in string values. Nongraphic characters are those which cannot be typed from the keyboard directly, for example, backspace, tab, carriage return, etc. These nongraphic characters can be represented by using escape sequences. An escape sequence is represented by a (backslash) followed by one or more characters.
Python allows you to have two string types, single-line strings, and multi-line strings.
Single line strings The strings you create by enclosing text in single quotes or double quotes are single-line strings i.e., they must terminate in one line.
Example: Valid single-line string
Str1 'hello
Str2-how are you
valid single-line string
str3-happy toe
See you'
Multiline strings Sometimes you need to store some text spread across multiple lines as one string. For that Python offers multiline strings which are created in two ways:-
1. By adding a backslash at the end of the normal single-quoted/ double-quoted strings.
Example: Valid multiline string
Str1 'hello\
Friends'
2. By typing the text in triple quotation marks (No backslash needed)
Example: Valid multiline string
Str2-'"Hello World
Welcome"'
Size of String
Each character of a Python string takes 1 byte of memory. Python determines the size of a string by counting the total character present in the string. Any escape sequence present innastring will take one byte of memory.
calculating the size.
Numeric Literal
There are four types of numeric literals: int (Integer), float (floating-point numbers), bool Boolean values, and complex. They have the features of their given data type. Following are examples of different types of numeric literal.
Special Literal
Python has one special literal called None. The None literal is used to indicate the
absence of a value. It is used to indicate the end of a list in Python.
The None value means "There is no useful information" or "There is nothing here"
value1-10
value2=None
print(value2)#displays None
Operators
They are tokens that trigger some computation when applied to variables or other
objects in an expression. Variables and objects to which the computation applies are
called operands. So an operator requires operands to Work on.
Unary operators:-
Unary operator+
Unary operator -
Binary operators
Arithmetic operators : ,/, *
Relational operators : S,, >==,!=
Logical operators : and, or, not
Membership operators : in,not in
Assignment operators : + = /=**=//
Punctuators
They are symbols used in programming to organize sentence structures.
Most common punctuators are:-
'(single quote)
"(double quote
# (hash)
\(backslash)
()(Parenthesis)
[](ISquare bracket
{}(Curly braces)
@(at the ratecomma)
:(colon)
.(dot)
Post a Comment