You are on page 1of 16

Induction to Python

Python is a powerful multi-purpose,object oriented programming language created by Guido van


Rossum and first released in 1991.It has simple easy-to-use syntax. You can use Python
programming language for developing both desktop and web applications. Python programs
generally are smaller than other programming languages like Java. Now a day‟s Python is very
demanding language to create different types of applications.

Features of Python

 Easy to Code
o It is very easy to write coding in Python.

 Easy to Learn
o Python is very easy to use and learn as it has simple syntax.

 Free and Open Source


o Python is freely available at official website and can be downloaded easily

 Object Oriented Language


o One of the key features of Python is Object-Oriented programming.
Python supports object-oriented language and concepts of classes,
objects encapsulation etc.

 GUI Programming Support


o It supports the Graphical User interface environment, which makes it
easier. Python is the most popular option for creating graphical apps with
Python.

 Extensible feature:
o Its facilities the user to write some of its coding in other languages such as
C, C++

 Portability:
o It is cross – platform language. It can run equally on different platforms,
for example, if we have python program for windows and if we want to run
this code on others platform such as Linux, Unix and Mac then we do not
need to change it.

 Python is integrated language:


o Python is an integrated language as we can easily integrate with other
languages such as C, C++
 A broad standard library
o Python has a large and broad library and provide large set of modules and
functions to develop applications

Different modes of Python:

1. Interactive Mode: Interactive mode is used when a user wants to run one single
line or one block of code. It runs very quickly and gives the output immediately.
"Interactive mode" is a "command line shell. This mode is takes less time
compared to script mode." But you can‟t save program in Interactive mode.

Figure(1.1)

It will show the output immediately after typing the command .In above
figure(1.1)we are printing a message and it immediately giving output in just next
line.

2. Script Mode: It is used when the user is working with more than one single code
or a block of code. It is called normal or editor mode .you can type up all your
code in one text file, or script, and run all the code at once.You can Save ,Edit
and reuse the program written in script mode.
F5 is the shortcut
How to work in script mode:
key to run Python
1. In the standard Python shell you can go toFile  New File. Program.

2. A blank script window will appear where you can write your code.
3. Then save the code(script) with a “.py” extension.
4. To run the script, either select “Run” -> “Run

Script Mode

Figure(1.2)

1. Program to print any message using script Mode.

Output:
Variables in Python:

Variables are used to store data values .It is like a container and reserve a memory
space to store values. We can apply various mathematical operations on variable. In
Python no need to define the data type of a variable, Python will automatically sets the
data type of a variable according to value stored in it.

Rules for declaring a variable

 Variable name must start with a letter or the underscore character. It cannot
start with a number.
 Variable name can only contain alpha-numeric characters and underscores
(A-Z, 0-9, and _ )
 Variable names are case-sensitive (name, Name and NAME are three
different variables)

We can assign one variable in single statement

We can assign single value to multiple variables

We can assign multiple variables in single statement

Character set in Python

1. Character
 Letters We can use these characters set in Python
 A–Z
 a–z We can use only (_) underscore special character in
 Digits variable name
 0-9
2. Special Symbols
 Space, ?, +, =, #, ! %, „, “”, $, < , >

3. White Spaces
 Blank spare, horizontal tab, carriage return, newline, form feed
Keywords

 Keywords are the reserved words, having special meaning that we use in
programming, they are case sensitive, below is the list of keywords

Tokens

Tokens are the smallest unit of the program. There are following tokens in Python

1. Reserved words or Keywords


 Keywords are nothing but a set of special words, which are some
special meaning and are reserved by Python.
2. Identifiers
 Identifiers in python are nothing but user-defined names to represent
programmable entity like variables, functions, classes, modules or any
other objects.
3. Literals
 Literals can be defined as data that is given in a variable or constant.
Python has following literals:

4. Operators
 Operators are the symbols which perform the operation on some
values. These values are known as operands.
Data types in Python:

In programming, data type is an important concept. Data types are the classification of
data items.

Data type defines, the type of data which we can store variables

Every variable in Python has a data type.Data type is used to define the type of the
data. Some of the data types are integer, string, float, character etc.

Python offers many built in data types.We are going to discuss some basic data types
used in Python

Setting the Data Type:

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

Example Data Type

x = “Welcome to Python” Str

x = 100 Int

x = 10.20 Float

x = 5k Complex

Number Datatype

Python Number data type is used to hold numeric values. Number Data type can belong
to following numerical types.

Int(signed integer) These are whole number can be positive or negative;


they have no fractional part, of unlimited length.
Example: 45,50,78, -55
Float(floating point Float (floating point real values) – float; or "floating
values) point number" is a number, positive or negative,
containing one or more decimals. Floats may also be in
scientific notation, with E or e indicating the power of 10
(2.5e2 = 2.5 x 102 = 250).
Complex (complex Python has built-in support for complex numbers,
Numbers) which are written with this latter notation; the
imaginary part is written with a j suffix, e.g., 3+1j. To
get access to complex equivalents of the math module,
use cmath.

# is used to give remarks in coding

Type function is used to get


data type of a variable
Str Data Type

Strings are sequences of character data. The string type in Python is called str.

We can use single quotes or double quotes to represent strings. Multi-line strings can
be denoted using triple quotes, ''' or """.

Print () Function

Print function is used to print the message / statement on the screen. It is also used to print the value of
a variable.
Escape Sequence in Python:

Escape sequences are nongraphic characters that cannot be typed directly from keyboard e.g.
Tab, backspace etc. these characters can be represented by escape sequences. And used with
the help of(\).

Escape
Description Example Output
Sequence
\\ Prints Backslash print "\\" \
\` Prints single-quote print "\'" '
\" Pirnts double quote print "\"" "
ASCII bell makes ringing the bell alert
\a print "\a" N/A
sounds ( eg. xterm )
ASCII backspace ( BS ) removes previous print "ab" + "\b" +
\b ac
character "c"
hello
\f ASCII formfeed ( FF ) print "hello\fworld"
world
hello
\n ASCII linefeed ( LF ) print "hello\nworld"
world
Prints a character from the Unicode print
\N{name} †
database u"\N{DAGGER}"
ASCII carriage return (CR). Moves all
characters after ( CR ) the the beginning of print
\r XX_XX6
the line while overriding same number of "123456\rXX_XX"
characters moved.
\t ASCII horizontal tab (TAB). Prints TAB print "\t* hello" * hello
\t ASCII vertical tab (VT). N/A N/A
\uxxxx Prints 16-bit hex value Unicode characterprint u"\u041b" Л
print
\Uxxxxxxxx Prints 32-bit hex value Unicode character Ʃ
u"\U000001a9"
\ooo Prints character based on its octal value print "\043" #
\xhh Prints character based on its hex value print "\x23" #
LinuxConfig.org
List:Lists are just like the arrays, declared in other languages .A single list may contain
DataTypes like Integers, Strings, as well as Objects. Lists are Changeable they can be altered
even after their creation. It represents comma separated values between Square brackets

([ ]).

OutPut

Tupels:

Tuples are sequences of values , just like lists. The differences between tuples and lists are,
the tuples cannot be changed unlike lists. The elements of Tuple is separated by comma and
enclosed in parentheses().

Output:

Dictionary:
Python dictionary is an unordered collection of items. a dictionary has a key: value pair with in
{ } brackets.Keys of a Dictionary must be unique and of immutable data type such as Strings,
Integers and tuples, but the key-values can be repeated and be of any type.

OutPut

Operators in Python

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators

Arithmetic Operators

These are the special symbols, which are used to perform mathematical operations on the operands.
(Operands can be value or a variable). Combination of operands and operator creates an expression.

Some of the mathematical operators are

Operator Symbol Description Output


+ Addition Gives sum of two given variables or values
- Subtraction Gives difference of two given variables or values
* Multiplication Gives product of two given variables or values
/ Division Divide 1st number with 2nd number and give result in
decimal
% Reminder / Modulus Gives remainder by dividing 1st no. with 2nd no.
// Integer Division Give quotient dividing 1st no. with 2nd no.
** Exponentiation Gives power of 1st No. with 2nd No.

Example of Operator (+)


Example of Operator (-)

Example of Operator (*)

Example of Operator (/)


Example of Operator (%)

Example of Operator (//)

Example of Operator (**)


Assignment Operator

Assignment operators are used to assign values to variable

(=)

= operator is used to assign value to a variable

Syntax x=5

Here we have assigned value 5 to variable x

Comparison Operator

Comparison operators are used to compare 2 values. It will return true if condition is
true otherwise it will return false

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

== Operator

x=9
y=3

print(x == y)

# returns False because 9 is not equal to 3

!= Operator

x=5

y=3

print(x != y)
# returns True because 5 is not equal to 3

>Operator

x=5
y=3
print(x > y)

# returns True because 5 is greater than 3

<Operator
x = 50
y=3
print(x < y)

# returns False because 50 is not less than 3

>=Operator
x = 50
y=3
print(x >= y)

# returns True because 50 is greater than 3

<=Operator
x = 50
y=3
print(x <= y)

# returns False because 50 is greater than 3

# returns true because five is greater, or equal, to 3


Logical Operator

Logical operators are used to combine conditional statements. This is used to apply
more than one condition on variables.

Operator Description Example


and Return true if both x < 5 and x > 2
conditions are true
or Return true if one of the x < 5 or x > 2
conditions is true
not If condition not matched not (x<5 and x<10)
return true, if matched
return false

and Operator

x=5

print(x > 3 and x < 15)

# returns true because 5 is greater than 3 AND 5 is less than 15

Or Operator

x=5

print(x > 3 and x <4)

# returns true because one of the condition is true 5 is greater than 3

Not Operator
x=5
print (not(x>3 and x<10))

# returns false because both given condition satisfies

You might also like