PYTHON FEATURES, SETTING UP PATH BASIC SYNTAX, COMMENTS, VARIABLE

FEATURES, SETTING UP PATH BASIC SYNTAX, COMMENTS, VARIABLE

IMAGE OF PYTHON FEATURES, SYNTAX, COMMENTS, VARIABLE

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language.

Features of Python

There are some important features of Python programming language...

1. Easy to Learn

This is one of the most important reasons for the popularity of Python. Python has a limited set of keywords. Its features such as simple syntax, usage of indentation to avoid clutter of curly brackets and dynamic typing that doesn't necessitate prior declaration of variable help a beginner to learn Python quickly and easily.

2. Interpreter Based

Any Programming languages is either compiler based or interpreter based. Python is an interpreter based language. The interpreter takes one instruction from the source code at a time, translates it into machine code and executes it. Instructions before the first occurrence of error are executed. With this feature, it is easier to debug the program and thus proves useful for the beginner level programmer to gain confidence gradually. Python therefore is a beginner-friendly language.

3. Interactive

Python prompt >>> works on the principle of REPL (Read – Evaluate – Print – Loop). You can type any valid Python expression here and press Enter. Python interpreter immediately returns the response and the prompt comes back to read the next expression like this >>>.

Example-1:

>>> 2*3+1

7

Example-2:

>>> print ("Hello World")

Hello World

The interactive mode is especially useful to get familiar with a library and test out its functionality. You can try out small code snippets in interactive mode before writing a program.

4. MultiParadigm

Python is a completely object-oriented language. Everything in a Python program is an object. However, Python conveniently encapsulates its object orientation to be used as an imperative or procedural language-such as C. Python also provides certain functionality that resembles functional programming. Moreover, certain third-party tools have been developed to support other programming paradigms such as aspect-oriented and logic programming.

5. Standard Library

Even though it has a very few keywords (only Thirty Five), Python software is distributed with a standard library made of large number of modules and packages. Thus Python has out of box support for programming needs such as serialization, data compression, internet data handling, and many more. Python is known for its batteries included approach.

6. Open Source and Cross Platform

Python's standard distribution can be downloaded from https://www.python.org/downloads/ without any restrictions. In addition, the source code is also freely available, which is why it comes under open source category.

Python is a cross-platform language. Pre-compiled binaries are available for use on various operating system platforms such as Windows, Linux, Mac OS, Android OS. A Python program can be easily ported from one OS platform to other.

7. GUI Applications

Python's standard distribution has an excellent graphics library called TKinter. It is a Python port for the vastly popular GUI toolkit called TCL/Tk. You can build attractive user-friendly GUI applications in Python. Examples are PyQt, WxWidgets, PySimpleGUI etc.

8. Database Connectivity

Almost any type of database can be used as a backend with the Python application. DB-API is a set of specifications for database driver software to let Python communicate with a relational database. With many third party libraries, Python can also work with NoSQL databases such as MongoDB.

9. Extensible

The term extensibility implies the ability to add new features or modify existing features. As stated earlier, CPython (which is Python's reference implementation) is written in C. Hence one can easily write modules/libraries in C and incorporate them in the standard library. There are other implementations of Python such as Jython (written in Java) and IPython (written in C#). Hence, it is possible to write and merge new functionality in these implementations with Java and C# respectively.

10. Active Developer Community

As a result of Python's popularity and open-source nature, a large number of Python developers often interact with online forums and conferences. Python Software Foundation also has a significant member base, involved in the organization's mission to "Promote, Protect, and Advance the Python Programming Language". Major IT companies Google, Microsoft, and Meta contribute immensely by preparing documentation and other resources.

More Features of Python

Apart from the above-mentioned features, Python has another big list of good features, few are listed below...

1. It supports functional and structured programming methods as well as OOP.

2. It can be used as a scripting language or can be compiled to byte-code for building large applications.

3. It provides very high-level dynamic data types and supports dynamic type checking.

4. It supports automatic garbage collection.

5. It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java. 

SETTING UP PATH FOR PYTHON

1. Get Python Installer from python.org.

2. Get the installer and an installation window will appear.

3. Press the “Add Python X.X to your PATH” option and install the python.

This way you can set up a default path without any headache.

BASIC SYNTAX

The Python syntax defines a set of rules that are used to create a Python Program. The Python Programming Language Syntax has many similarities to Perl, C, and Java Programming Languages. However, there are some definite differences between the languages.

Modes of Python Programming

There are two different modes of Python Programming...

1. Interactive Mode Programming

2. Script Mode Programming.

Let's execute a Python program to print "Hello, World!" in both modes... 

1. Interactive Mode Programming

>>>

Here >>> denotes a Python Command Prompt where you can type your commands. Let's type the following text at the Python prompt and press the Enter...

>>> print ("Hello, World!")

Result-

Hello, World!

2. Script Mode Programming

We can write a simple Python program in a script which is simple text file. Python files have extension .py. Type the following source code in a test.py file...

print ("Hello, World!")

We assume that you have Python interpreter path set in PATH variable. Now, let's try to run this program as follows...

test.py

Result-

Hello, World!

PYTHON COMMENTS

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Creating a Comment

Comments starts with a #, and Python will ignore them.

Example:

#This is a comment

print("Hello, World!")

Single line Comment

Comments can be placed at the end of a line, and Python will ignore the rest of the line.

Example:

print("Hello, World!") #This is a comment

A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code.

Example:

#print("Hello, World!")

print("Cheers, Mate!")

Multiline Comments

Python does not really have a syntax for multiline comments.

To add a multiline comment you could insert a # for each line.

Example:

#This is a comment

#written in

#more than just one line

print("Hello, World!")

Or, not quite as intended, you can use a multiline string.

Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it.

Example:

"""

This is a comment

written in

more than just one line

"""

print("Hello, World!")

As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multi-line comment.

VARIABLE

Variables are the containers for storing data values.

Creating Variables

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Example:

x = 5

y = "totalsach"

print(x)

print(y)

Variables do not need to be declared with any particular type, and can even change type after they have been set.

Example:

x = 4       # x is of type int

x = "Sally" # x is now of type str

print(x)

Get the Type

You can get the data type of a variable with the type() function.

Example:

x = 5

y = "totalsach"

print(type(x))

print(type(y))

Single or Double Quotes?

String variables can be declared either by using single or double quotes.

Example:

x = "totalsach"

# is the same as

x = 'totalsach'

Case-Sensitive

Variable names are case-sensitive.

Example:

This will create two variables:

a = 4

A = "totalsach"

#A will not overwrite a

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for Python variables

1. A variable name must start with a letter or the underscore character.

2. A variable name cannot start with a number.

3. A variable name can only contain alpha-numeric characters and underscores (A-Z, 0-9, and _).

4. Variable names are case-sensitive (age, Age and AGE are three different variables).

5. A variable name cannot be any of the Python keywords.

6. Variable names are case-sensitive.

Legal variable names

myvar = "totalsach"

my_var = "totalsach"

_my_var = "totalsach"

myVar = "totalsach"

MYVAR = "totalsach"

myvar2 = "totalsach"

Illegal variable names

2myvar = "totalsach"

my-var = "totalsach"

my var = "totalsach"

Multi Words Variable Names

Variable names with more than one word can be difficult to read.

There are several techniques you can use to make them more readable...

Camel Case

Each word, except the first, starts with a capital letter:

myVariableName = "totalsach"

Pascal Case

Each word starts with a capital letter:

MyVariableName = "totalsach"

Snake Case

Each word is separated by an underscore character.

my_variable_name = "totalsach"

Python Variables - Assign Multiple Values

1. Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one line:

Example:

x, y, z = "Orange", "Banana", "Cherry"

print(x)

print(y)

print(z)

Note: Make sure the number of variables matches the number of values, or else you will get an error.

2. One Value to Multiple Variables

And you can assign the same value to multiple variables in one line.

Example:

x = y = z = "Orange"

print(x)

print(y)

print(z)

Unpack a Collection

If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.

Example:

Unpack a list:

fruits = ["apple", "banana", "cherry"]

x, y, z = fruits

print(x)

print(y)

print(z)

Output Variables

The Python print() function is often used to output variables.

Example:

x = "Python is awesome"

print(x)

In the print() function, you output multiple variables, separated by a comma.

Example:

x = "Python"

y = "is"

z = "awesome"

print(x, y, z)

You can also use the + operator to output multiple variables.

Example:

x = "Python "

y = "is "

z = "awesome"

print(x + y + z)

Notice the space character after "Python " and "is ", without them the result would be "Pythonisawesome".

For numbers, the + character works as a mathematical operator.

Example:

x = 5

y = 10

print(x + y)

In the print() function, when you try to combine a string and a number with the + operator, Python will give you an error.

Example:

x = 5

y = "totalsach"

print(x + y)

The best way to output multiple variables in the print() function is to separate them with commas, which even support different data types.

Example:

x = 5

y = "totalsach"

print(x, y)

Global Variables

Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside.

Example:

Create a variable outside of a function, and use it inside the function.

x = "awesome"

def myfunc():

print("Python is " + x)

myfunc()

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

Example:

Create a variable inside a function, with the same name as the global variable

x = "awesome"

def myfunc():

x = "fantastic"

print("Python is " + x)

myfunc()

print("Python is " + x)

The global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.

Example:

If you use the global keyword, the variable belongs to the global scope:

def myfunc():

global x

x = "fantastic"

myfunc()

print("Python is " + x)

Also, use the global keyword if you want to change a global variable inside a function.

Example:

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

x = "awesome"

def myfunc():

global x

x = "fantastic"

myfunc()

print("Python is " + x)

ALSO READ:

3. DIFFERENT DATA TYPES

4. CASTING, STRING, BOOLEAN

5. PYTHON OPERATORS

6. CONDITIONAL STATEMENTS

7. LOOPING

8. CONTROL STATEMENTS, STRING MANIPULATION, LISTS, TUPLE, SETS

9. DICTIONARIES

10. ARRAYS

11. ITERATORS, MODULES, DATES, MATH

12. MODULES, INPUT AND OUTPUT

13. FUNCTION & ARGUMENTS

14. MODULES

15. EXCEPTION HANDLING

16. BUILT IN FUNCTION IN PYTHON

17. FILE HANDLING IN PYTHON

18. PYTHON ARCHITECTURE

19. DOCUMENTATION IN PYTHON

Total सच

एक टिप्पणी भेजें

और नया पुराने