0% found this document useful (0 votes)
16 views

Introduction To Python Programming

It contains introduction of python programming languages and its basic synax and programs.

Uploaded by

Vishal Kawale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Introduction To Python Programming

It contains introduction of python programming languages and its basic synax and programs.

Uploaded by

Vishal Kawale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction to Python Programming

Python is a high-level, general-purpose programming


language.Python is dynamically typed and garbage-collected. It
supports
multiple programmingparadigms,including structured (particularly
procedural), object-oriented and functional programming.Guido
van Rossum began working on Python in the late 1980s as a
successor to the ABC programming language and first released it in
1991 as Python 0.9.0.[37] Python 2.0 was released in 2000.
Python 3.0, released in 2008, was a major revision not
completely backward-compatible with earlier versions.
Python 2.7.18, released in 2020, was the last release of Python
Python is a dynamic, high-level, free open source, and interpreted
programming language. It supports object-oriented programming
as well as procedural-oriented programming. In Python, we don’t
need to declare the type of variable because it is a dynamically
typed language. For example, x = 10 Here, x can be anything such
as String, int, etc.

Features in Python
1.Free and Open Source
2. Easy to code
3. Easy to Read
4. Object-Oriented Language
5. GUI Programming Support
6. High-Level Language
7. Large Community Support
8. Easy to Debug
9. Python is a Portable language
10. Python is an Integrated language

1. 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. Download Python Since it is
open-source, this means that source code is also available to the
public. So you can download it, use it as well as share it.
2. 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 the Python language
and anybody can learn Python basics in a few hours or days. It is
also a developer-friendly language.

3. Easy to Read

As you will see, learning Python is quite simple. As was already


established, Python’s syntax is really straightforward. The code
block is defined by the indentations rather than by semicolons or
brackets.

4. Object-Oriented Language

One of the key features of Python is Object-Oriented programming.


Python supports object-oriented language and concepts of classes,
object encapsulation, etc.

5. 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.

6. 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.

7. Large Community Support

Python has gained popularity over the years. Our questions are
constantly answered by the enormous StackOverflow community.
These websites have already provided answers to many questions
about Python, so Python users can consult them as needed.

8. Easy to Debug

Excellent information for mistake tracing. You will be able to


quickly identify and correct the majority of your program’s issues
once you understand how to interpret Python’s error traces. Simply
by glancing at the code, you can determine what it is designed to
perform.

9. Python is a 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.

10. Python is an Integrated language

Python is also an Integrated language because we can easily


integrate Python with other languages like C, C++, etc.

11. 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.

12. Large Standard Library

Python has a large standard library that provides a rich set of


modules and functions so you do not have to write your own code
for every single thing. There are many libraries present in Python
such as regular expressions, unit-testing, web browsers, etc.

13. 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.

14. Frontend and backend development

With a new project py script, you can run and write Python codes in
HTML with the help of some simple tags <py-script>, <py-env>,
etc. This will help you do frontend development work in Python like
javascript. Backend is the strong forte of Python it’s extensively
used for this work cause of its frameworks like Django and Flask.
15. Allocating Memory Dynamically

In Python, the variable data type does not need to be specified.


The memory is automatically allocated to a variable at runtime
when it is given a value. Developers do not need to write int y =
18 if the integer value 15 is set to y. You may just type y=18.

History of Python
Python was developed by Guido van Rossum in the late eighties
and early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC,
Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other
scripting languages.
Python is copyrighted. Like Perl, Python source code is now
available under the GNU General Public License (GPL).
Python is now maintained by a core development team at the
institute, although Guido van Rossum still holds a vital role in
directing its progress.

Python Variables
Creating Variables

1.Python has no command for declaring a variable.

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

x = 5
y = "John"
print(x)
print(y)

3.Variables do not need to be declared with any particular type, and can
even change type after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)

4.Casting

If you want to specify the data type of a variable, this can be done with
casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0

5.Get the Type

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

x = 5
y = "John"
print(type(x))
print(type(y))

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


x = "John"
# is the same as
x = 'John'

6.Case-Sensitive

Variable names are case-sensitive.

This will create two variables:

a = 4
A = "Sally"
#A will not overwrite a

Python - 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.

Python Data Types

Built-in Data Types


In programming, data type is an important concept.

Variables can store data of different types, and different types can do
different things.

Text Type: str

Numeric int, float, complex


Types:

Sequence list, tuple, range


Types:

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

None Type: NoneType

Python has the following data types built-in by default, in these categories:

Getting the Data Type


In Python, the data type is set when you assign a value to a variable:

x = "Hello World" str

x = 20 int

x = 20.5 float

x = 1j complex
x = ["apple", "banana", "cherry"] list

x = ("apple", "banana", "cherry") tuple

x = range(6) range

x = {"name" : "John", "age" : 36} dict

x = {"apple", "banana", "cherry"} set

x = frozenset({"apple", "banana", "cherry"}) frozenset

x = True bool

x = b"Hello" bytes

x = bytearray(5) bytearray

x = memoryview(bytes(5)) memoryview

x = None NoneType

Setting the Specific Data Type


If you want to specify the data type, you can use the following constructor
functions:

Example Data Type

x = str("Hello World") str

x = int(20) int

x = float(20.5) float

x = complex(1j) complex

x = list(("apple", "banana", "cherry")) list

x = tuple(("apple", "banana", "cherry")) tuple

x = range(6) range

x = dict(name="John", age=36) dict

x = set(("apple", "banana", "cherry")) set

x = frozenset(("apple", "banana", "cherry")) frozenset


x = bool(5) bool

x = bytes(5) bytes

x = bytearray(5) bytearray

x = memoryview(bytes(5)) memoryview

Python Operators
Python divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators

Python Arithmetic Operators


Arithmetic operators are used with numeric values to perform common
mathematical operations:

Operator Name Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y
Python Assignment Operators
Assignment operators are used to assign values to variables:

Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

//= x //= 3 x = x // 3

**= x **= 3 x = x ** 3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

Python Comparison Operators


Comparison operators are used to compare two values:

Operator Name Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

Python Logical Operators


Logical operators are used to combine conditional statements:

Operator Description Example

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is x < 5 or x < 4


true

not Reverse the result, returns False if the not(x < 5 and x < 1
result is true

Python Identity Operators


Operator Description Example

is Returns True if both variables are the same x is y


object

is not Returns True if both variables are not the x is not y


same object
Identity operators are used to compare the objects, not if they are equal,
but if they are actually the same object, with the same memory location:
Python Membership Operators
Membership operators are used to test if a sequence is presented in an
object:
Operator Description Example

in Returns True if a sequence with the specified value x in y


is present in the object

not in Returns True if a sequence with the specified value x not in y


is not present in the object

Python Bitwise Operators


Bitwise operators are used to compare (binary) numbers:
Operat Name Description Example
or

& AND Sets each bit to 1 if both bits are 1 x&y

| OR Sets each bit to 1 if one of two bits is 1 x|y

^ XOR Sets each bit to 1 if only one of two bits is 1 x^y

~ NOT Inverts all the bits ~x

<< Zero fill Shift left by pushing zeros in from the right and let x << 2
left shift the leftmost bits fall off

>> Signed Shift right by pushing copies of the leftmost bit in x >> 2
right shift from the left, and let the rightmost bits fall off

Python Conditional Statements

# python program to illustrate nested If statement

i = 10
if (i == 10):

# First if statement
if (i < 15):
print("i is smaller than 15")

# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and
works more like an iterator method as found in other object-orientated
programming languages.

With the for loop we can execute a set of statements, once for each item
in a list, tuple, set etc.

Example
Print each fruit in a fruit list:

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


for x in fruits:

print(x)

for x in "banana":
print(x)

You might also like