0% found this document useful (0 votes)
178 views4 pages

Python Fundamentals

Python is a versatile programming language designed to be readable and easy to develop. It has a rich library that allows building sophisticated applications using relatively simple code. The document discusses fundamentals of Python including using the interactive and command-line interpreters, variables, displaying and taking input, keywords, operators, expressions, strings, numbers, and built-in functions.

Uploaded by

unknown45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
178 views4 pages

Python Fundamentals

Python is a versatile programming language designed to be readable and easy to develop. It has a rich library that allows building sophisticated applications using relatively simple code. The document discusses fundamentals of Python including using the interactive and command-line interpreters, variables, displaying and taking input, keywords, operators, expressions, strings, numbers, and built-in functions.

Uploaded by

unknown45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

FUNDAMENTALS OF PYTHON

1) Overview of Python

The Python programming language was developed in the late 1980s by Dutch programmer Guido van Rossum. The
language was not named after the large snake species but rather after the BBC comedy series Monty Python’s Flying
Circus. Guido van Rossum happens to be a fan. Just like the Linux OS, Python eventually became an open source
software development project. However, Guido van Rossum still has a central role in deciding how the language is
going to evolve. To cement that role, he has been given the title of “Benevolent Dictator for Life” by the Python
community.
Python is a versatile and easy-to-use language that was specifically designed to make programs readable and easy to
develop. Python also has a rich library making it possible to build sophisticated applications using relatively simple-
looking, small amount of code. For these reasons, Python has become a popular application development language.

2) Using Python

We use the Python language by working with the Python interpreter. Two common ways the Python interpreter can be
used are the interactive mode and the command-line mode, which are described below.

2.1) Interactive mode


We first invoke (start) the Python interpreter and then work with it interactively. That is, we give the interpreter
Python commands, one at a time. The interactive mode makes it easy to experiment with features of the language, or
to test functions during program development. It is also a handy desk calculator. We will use the interactive mode to
introduce and demonstrate Python language features in this document, when it is more appropriate.

2.2) Command-line Mode


In this non-interactive mode, we give the Python interpreter a text file containing a Python program (also called a
Python script) as input. Python source files are given the filename extension “.py”. A Python program can consist of
several lines of Python code. A programmer uses a text editor to create and modify files containing the Python source
code. We will use this command-line mode more towards the later parts.

3) Python Variables

Python variables do not have to be explicitly declared 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.

Ex :- counter = 100 # An integer assignment


miles = 1000.32 # A floating point assignment
name = "John" # A string assignment
a=b=c=1 # A Multiple assignment
a, b, c = 1, 2, „john‟ # A Multiple type assignment

4) Displaying Texts

my_msg = "Hello World !" Output : Hello World !


print "Let‟s Learn Python." Let‟s Learn Python.
print my_msg

5) Taking Inputs

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:
(i) raw_input( )
(ii) input( )

1
5.1) The raw_input Function
The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing
newline). Here is an example program that uses it.

num1 = raw_input(“Enter 1st Number: “)


num2 = raw_input(“Enter 2nd Number: “)
print “Num1+Num2 =”, num1+ num2

5.2) The 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. Here is a program that uses it.

num1 = input(“Enter Number1: “)


num2 = input(“Enter Number2: “)
print “Num1+Num2 = “, num1+ num2

6) Python Keywords

and del from not While as elif global


or with assert else If pass yield break
except import print class Exec raise continue finally
in is return for lambda try def
The above should not be used arbitrarily in programs, e.g., as names of variables. There are also a number of words
that have special meanings in Python, for e.g., True, False, None.

7) Operators and Expressions in Python

 Arithmetic operators ( + , - , * , / , % , ** , // )
 Comparison (i.e., relational) operators ( == , != , <> , > , < , >= , <= )
 Assignment operators ( = , += , -= , *= , /= , %= , **= , //= )
 Bitwise operators ( & , | , ^ , ~ , << , >> )
 Logical operators ( and , or , not )
 Membership operators ( in , not in )
 Identity operators ( is , is not )

7.1) Operator Precedence in Python

Operator Description
** Exponentiation (raise to the power)
~ , +, - Complement, unary plus and minus (method names for
the last two are +@ and -@)
*, / , %, // Multiply, divide, modulo and floor division
+,- Addition and subtraction
>>, << Right and left bitwise shift
& Bitwise 'AND'
^,| Bitwise exclusive `OR' and regular `OR'
<=, < , >, >= Comparison operators
< >, = =, != Equality operators
= , %=, /=, //=, -=, +=, *=, **= Assignment operators
is, is not Identity operators
in, not in Membership operators
not, or, and Logical operators

2
8) Escape Characters 9) String Formatting Operator

10) Python Numbers

Number data types store numeric values. They are immutable, which means once created their value never changes.
This means, changing the value of a numeric data type results in a newly allocated object. Python supports four
different numerical types:

I. int (signed integers): often called just integers or ints, are positive or negative whole numbers with no
decimal point.
II. long (long integers ): or longs, are integers of unlimited size, written like integers and followed by an
uppercase or lowercase L.
III. float (floating point, real values) : or floats, represent real numbers and are written with a decimal
point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating
the power of 10 (2.5e2 = 2.5 x 102 = 250).
IV. complex (complex numbers) : are of the form a + bJ, where a and b are floats and J (or j)
represents the square root of -1 (which is an imaginary number). a is the real part of the number, and b is the
imaginary part. Complex numbers are not used much in Python programming.

10.1) Number Type Conversion


Python converts numbers internally in an expression containing mixed types to a common type for evaluation. But
sometimes, we need to coerce a number explicitly from one type to another to satisfy the requirements of an operator
or function parameter.
 Type int(x) to convert x to a plain integer.
 Type long(x) to convert x to a long integer.
 Type float(x) to convert x to a floating-point number.
 Type complex(x) to convert x to a complex number with real part x and imaginary part zero.
 Type complex(x, y) to convert x and y to a complex number with real part x and imaginary part y. x
and y are numeric expressions.

11) Built-In Functions in Python

abs() divmod() input() open() staticmethod()


all() enumerate() int() ord() str()
any() eval() isinstance() pow() sum()
basestring() execfile() issubclass() print() super()
bin() file() iter() property() tuple()
bool() filter() len() range() type()

3
bytearray() float() list() raw_input() unichr()
callable() format() locals() reduce() unicode()
chr() frozenset() long() reload() vars()
classmethod() getattr() map() repr() xrange()
cmp() globals() max() reversed() zip()
compile() hasattr() memoryview() round() __import__()
complex() hash() min() set() apply()
delattr() help() next() setattr() buffer()
dict() hex() object() slice() coerce()
dir() id() oct() sorted() intern()

11.1) List Functions 11.2) Mathematical Functions

11.3) Trigonometric Functions

11.4) String Function s

You might also like