Python programming for beginners - Introduction to python programming
Do you want to conquer the top position in the field of computer programming. Learn python! Introduction to python for beginners will help you to conquer your dream. Here we learn all the basics about Python Language.
Python was developed by Guido van Rossum at the National Research Institute for Mathematics and Computer science in Netherlands during 1985-1990. Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, UNIX shell and other scripting languages. Rossum was inspired by Monty python’s Flying Circus, a BBC Comedy series and he wanted the name of his new language to be short, unique and mysterious. Hence he named it Python. It is a general purpose interpreted, interactive, object oriented, and high level programming language. Python source code is available under the GNU General Public License (GPL) and it is now maintained by a core development team at the National Research Institute.
- Features of Python
- Identifiers
- Reserved Keywords
- Variables
- Comments in python
- Indentation in python
- Multi-Line Statements
- Multiple statement Group(SUIT)
- Quotes in Python
- Input, Output and Import Functions
- Displaying the output
- Reading the input
- Import function
1. Features of Python
Simple and easy-to-learn
Interpreted and Interactive
Object-Oriented
Portable
Scalable
Extendable
Dynamic
GUI Programming and Databases
Broad Standard Library
Python is simple language with few keywords, Simple structure and its syntax is also clearly defined. This makes python a beginner’s language.
Python is processed at runtime by the interpreter. We need not compile the program before executing it. The Python prompt interact with the interpreter to interpret the programs that we have written. Python has an option namely interactive mode which allows interactive testing and debugging of code.
python supports Object Oriented Programming (OOP) concepts that encapsulate code within objects. All concepts in OOPs like data hiding operator overloading Inheritance etc. can be well written in Python. It supports functional as well as structured programming.
Python can run on a wide variety of hardware and software platforms and has the same interface on all platforms. All variants of Windows, UNIX, Linux and Macintosh are to name a few.
Python provides a better structure and support for large programs than shell scripting. It can be used as a scripting language are can be compiled to byte code (intermediate code that is platform Independent) for building large applications.
You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient. It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Python provides very high-level dynamic data types and supports dynamic type checking. It also supports automatic garbage collection.
Python supports GUI applications that can be created and ported to many libraries and windows systems, such as Windows Microsoft Foundation Classes (MFC), Macintosh the X Window system of Unix Python also provides interfaces to all major commercial databases.
Python's library is portable and cross platform compatible on UNIX, Linux, Windows and Macintosh. This helps in the support and development of a wide range of applications from simple text processing to browsers and complex games.
2. Identifiers
A Python identifier is a name used to identify a variable, function, class, module or any other object. Python is case sensitive and hence uppercase and lowercase letters are considered distinct. The following are the rules for naming an identifier in Python.
- Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example Total and total is different.
- Reserved keywords cannot be used as an identifier.
- Identifiers cannot begin with a digit. For example 2more, 3times etc. are invalid identifiers.
- Special symbols like @, !, #, $, % etc. cannot be used in an identifier.
- Identifier can be of any length.
Examples for valid identifiers are – total, max_mark, count2 etc.
3. RESERVED KEYWORDS
These are keywords reserved by the programming language and prevent the user or the programmer from using it as an identifier in a program. There are 33 keywords in Python 3.3. This number may vary with different versions. To retrieve the keywords in Python the following code can be given at the prompt. All keywords except True, False and None are in lowercase.
4. VARIABLES
Variables are reserved memory locations to store values. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can stare integers, decimals or characters in these variables. Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the operator is the value stored in the variable.
5. COMMENTS IN PYTHON
Comments are very important while writing a program. It describes what the source code has done. Comments are for programmers for better understanding of a program. In Python, we use the hash (#) symbol to start writing a comment. A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment. It extends up to the newline character. Python interpreter ignores comment.
For multiline comments one way is to use (#) symbol at the beginning of each line. The following example shows a multiline comment. Another way of doing this is to use triple quotes, either ‘’’ or ‘’ ’’ ’’
6. INDENTATION IN PYTHON
Most of the programming languages like C, C++ and Java use braces { } to define a block of code. Python uses indentation. A code block (body of a function, loop etc.) starts with indentation and ends with the first unindented line. The amount of indentation can be decided by the programmer, but it must be consistent throughout the block. Generally four white spaces are used for indentation and is preferred over tab space.
7. MULTI-LINE STATEMENTS
Instructions that a Python interpreter can execute are called statements. For example, a=1 is an assignment statement. if statement, for statement, while statement etc. are other kinds of statements which will be discussed in coming Chapters. In Python, end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (\).
8. MULTIPLE STATEMENT GROUP (SUITE)
A group of individual statements, which make a single code block is called a suite in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite. Header lines begin the statement (with the keyword) and terminate with a colon (:) and are followed by one or more lines which make up the suite.
9. QUOTES IN PYTHON
Python accepts single (‘), double (") and triple ('" or "’’’’) quotes to denote string literals. The same type of quote should be used to start and end the string. The triple quotes are used to span the string across multiple lines.
10. INPUT, OUTPUT AND IMPORT FUNCTIONS
10.1 Displaying the Output
The function used to print output on a screen is the print statement where you can pass zero or more expressions separated by commas. The print function converts the expressions you pass into a string and writes the result to standard output.
By default a space is added after the text and before the value of variable a. The syntax of print function is given below. Print (*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False) Here, objects are the values to be printed. Sep is the separator used between the values. Space character is the default separator. end is printed after printing all the values. The default value for end is the new line. file is the object where the values are printed and its default value is sys.stdout (screen). Flush determines whether the output stream needs to be flushed for any waiting output. A True value forcibly flushes the stream.
10.2 Reading the Input
Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are raw_input and input. The raw_input function is not supported by Python 3.
raw_input function
The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing newline). This prompts you to enter any string and it would display same string on the screen.
input function
The input ([prompt]) function is equivalent to raw_input, except that it assumes the input is a valid Python expression and returns the evaluated result to you.
10.3 Import function
When the program grows bigger or when there are segments of code that is frequently used, it can be stored in different modules. A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py. Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword to do this. For example, we can import the math module by typing in import math.
Thank you so much everyone. If you find it useful, don't forget to share it with your friends.
Comments
Post a Comment