1.introduction To PYTHON

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

1.

INTRODUCTION TO PYTHON

Python is a powerful modern computer programming language. It bears some similarities to


Fortran, one of the earliest Programming languages, but it is much more powerful than
Fortran. Python was developed by Guido van Rossum, and it is free software. Free as in
“free beer,” in that you can obtain Python without spending any money. Python is a good
choice for mathematical calculations, since we can write code quickly, test it easily, and its
syntax is similar to the way mathematical ideas are expressed in the mathematical literature.
By learning Python you will also be learning a major tool used by many web developers.
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python
was designed to be highly readable which uses English keywords frequently whereas other
languages use punctuation and it has fewer syntactical constructions than other languages.

FEATURES OF PYTHON
 Python is Interpreted
It means that it is processed at runtime by the interpreter and you do not need to compile your
program before executing it.
 Python is Interactive
It means that you can actually sit at a Python prompt and interact with the interpreter directly to
write your programs.
 Python is Object-Oriented
 It means that Python supports Object-Oriented style or technique of programming that
encapsulates code within objects.
Python is Beginner's Language
Python is a great language for the beginner programmers and supports the development of a
wide range of applications from simple text processing to www browsers to games.

ADVANTAGES OF PYTHON
 Easy-to-maintain
Python's success is that its source code is fairly easy-to-maintain.
 A Broad Standard Library
One of Python's greatest strengths is the bulk of the library is very portable and cross-platform
compatible on UNIX, Windows and Macintosh.
 Interactive Mode
Support for an interactive mode in which you can enter results from a terminal right to the
language, allowing interactive testing and debugging of snippets of code.
 Portable
Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
 Extendable
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.
 Databases
Python provides interfaces to all major commercial databases.
1
 GUI Programming
Python supports GUI applications that can be created and ported to many system
calls, libraries and windows systems, such as Windows MFC, Macintosh and the X
Window system of Unix.
 Scalable
Python provides a better structure and support for large programs than shell scripting.

Apart from the above-mentioned features, Python has a big list of good features, few of them
are-
- Support for functional and structured programming methods as well as OOP.
- It can be used as a scripting language or can be compiled to byte-code for building large
applications.
- Very high-level dynamic data types and supports dynamic type checking.
- Supports automatic garbage collection.
- It can be easily integrated with C, C++, COM, ActiveX, CORBA and Java.

DISADVANTAGES OF PYTHON

1. Not for fastest use


2. Lesser libraries compared to other high level languages
3. Not strong on type binding
4. not easily convertible

PYTHON FUNDAMENTALS

We begin our study of Python by learning about its lexical structure and the
Python’s lexical structure comprises five lexical categories. Python uses to
translate code into symbols and punctuation

PYTHON CHARACTER SET


2
Python encodes characters using Unicode, which includes over 100,000 different characters
from 100 languages —including natural and artificial languages like mathematics. Character
set inclues LETTERS DIGITS SPECIAL SYMBOLS SPACES and OTHER CHARECTERS

PYTHON TOKENS

THE SMALLEST INDIVIDUAL UNIT INA PROGRAM IS TOKEN OR LEXICAL UNIT is


called TOKENS.

KEYWORDS IDENTIFIERS(NAMES) LITERALS are the part of tokens.

PYTHON IDENTIFIERS
We use identifiers in Python to define the names of objects.

There are also three semantic rules concerning Python identifiers.


ˆ
Identifiers are case-sensitive: identifiers differing in the case (lower or
upper) of their characters are different identifiers: e.g., Sal and sal
are different identifiers.
ˆ
Underscores are meaningful: identifiers can have only one special character
Underscore(_): e.g.,gross_sal

PYTHON KEYWORDS

Keywords are the reserved words in Python. We cannot use a keyword as variable name,
function name or any other identifier.

3
CONSTANTS

A constant is a type of variable whose value cannot be changed. It is helpful to think of


constants as containers that hold information which cannot be changed later.

PI = 3.14
GRAVITY = 9.8

LITERALS
STRING LITERALS
EG “psg”,”python”, “123DDD”, “a”

STRING TYPES

 SINGLE LINE
 MULTILINE
Example (multiline)

Str1=“hello\
Welcome”
If slash is not given error will be encountered.

Example (multiline)

Str1=“””hello
Welcome”””

Single quotes or double quotes(3 times) Will allow multiline

NUMERIC LITERALS
Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different
numerical types Integer, Float and Complex.

4
INT (WHOLE NUMBERS (4 bytes– SIGNED INTEGERS)

LONG ( LONG INTEGERS-UNLIMITED SIZE INT FOLLOWED WITH CAPITAL OR


SMALL)
FLOAT (FLOATING POINT REAL VALUES)
COMPLEX (COMPLEX NUMBERS)

BOOLEAN LITERALS
 TRUE
 FALSE
 NONE

OPEARTORS

 ARITHMETIC
 RELATIONAL
 ASSIGNMENT
 LOGICAL
 MEMBERSHIP OPERATORS

ARITHMETIC OPERATORS

Arithmetic operators are used to perform mathematical operations like addition,


subtraction, multiplication etc.

5
COMPARISON OPERATORS

Comparison operators are used to compare values. It either returns True or False according
to the condition.

LOGICAL OPERATORS

Logical operators are the and, or, not operators.

ASSIGNMENT OPERATORS

Assignment operators are used in Python to assign values to variables.

a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a
on the left.

There are various compound operators in Python like a += 5 that adds to the variable and
later assigns the same. It is equivalent to a = a + 5.

6
IDENTITY OPERATORS

is and is not are the identity operators in Python. They are used to check if two values (or
variables) are located on the same part of the memory. Two variables that are equal does not
imply that they are identical.

MEMBERSHIP OPERATORS

in and not in are the membership operators in Python. They are used to test whether a value
or variable is found in a sequence (string, list, tuple, set and dictionary).

In a dictionary we can only test for presence of key, not the value.

PUNCTUATORS
‘ “ ( ) [ ] {}, : =

BAREBONES OF PYTHON
 EXPRESSIONS (A+B)
 COMMENTS (# -SINGLE ‘’’-MULTI)
 STATEMENTS ( PRINT “HELLO”, IF A>B)
 FUNCTIONS (DOIT())
 BLOCKS AND INDENTATION

BLOCKS AND INDENTATION Example


IF A>B: #Block
PRINT (“GREATER”) #Indentation

7
VARIABLE ASSIGNMENT in PYTHON

A,B,C=12,4,5
a,b,c=12,6,7

a,b,c=a+1,b+2,c+3
print a,b,c

x,x=20,30
y,y=x+10,x+20
print x,y

SIMPLE PROGRAMS

# This program prints Hello, world!

print('Hello, world!')

Output

Hello, world!

# This program adds two numbers

# Store input numbers

num1 = input('Enter first number: ')

num2 = input('Enter second number: ')

# Add two numbers

sum = float(num1) + float(num2)

# Display the sum

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Output

Enter first number: 1.5


Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

8
# Python Program to find the area of triangle

a=5

b=6

c=7

# Uncomment below to take inputs from the user

# a = float(input('Enter first side: '))

# b = float(input('Enter second side: '))

# c = float(input('Enter third side: '))

# calculate the semi-perimeter

s = (a + b + c) / 2

# calculate the area

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print('The area of the triangle is %0.2f' %area)

Output

The area of the triangle is 14.70

# Python program to swap two variables

# To take input from the user

# x = input('Enter value of x: ')

# y = input('Enter value of y: ')

x=5

y = 10
9
# create a temporary variable and swap the values

temp = x

x=y

y = temp

print('The value of x after swapping: {}'.format(x))

print('The value of y after swapping: {}'.format(y))

Output

The value of x after swapping: 10


The value of y after swapping: 5

# Python Program to convert temperature in celsius to fahrenheit

# change this value for a different result

celsius = 37.5

# calculate fahrenheit

fahrenheit = (celsius * 1.8) + 32

print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))

Output

37.5 degree Celsius is equal to 99.5 degree Fahrenheit

Answer the following:

1. Write a Python program to find the average of 4 numbers.


2. Write a Python program to Print the quotient and remainder of a number.
3. Write a Python program to convert kilometres to Meters.
4. Write a Python program to convert Fahrenheit to Centigrade.
5. Write a Python program to find the area of a circle.
6. Write a Python program to print multiple strings.

10
7. What are tokens?
8. What is an identifier?
9. Explain about the barebones of Python.
10. How many string types are accepted in Python? Explain with an example

11

You might also like