Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Python Tutorial Part-1

Introduction

Python is a general purpose high level language.

Python was developed by Gudio Van rossam in 1989 while working at National Research Institute in Netherlands.

But officially python was made available to public in 1991

Example to Print HelloWorld

To print sum of two numbers

The name Python was selected from a TV Show

The complete Monty Python’s Circus” was broadcasted by BBC from 1969 to 1974.

Guido developed Python language by taking almost all Programming features from different languages.

·         Functional Programming from c.

·         OOPS from C++

·         Scripting Languages from perl and shellscript

·         Modular language features from Modular-3

 Where we can use python

For Developing desktop Applications

For Developing web Applications

For Developing database Applications

For Data Analysis

For Machine Learning

Features of Python

·         Simple and easy to learn

·         Open source

·         High level Programming Language

·         Platform Independent

·         Portability

·         Dynamically Types

·         Procedured Oriented and Object oriented

·         Interpreted

·         Extensible

·         Embeded

 

Limitations of Python

Performance wise is not up to the mark

It is not used to build Mobile Applications

 

Identifiers

A name in python program is called an identifier.

It can be class name or function name or variable name or module name

 

Rules to define Identifiers in Python

·         Allowed Characters – Alphabet symbols, digits, underscore.

·         Identifiers are case sensitive.

·         It should not start with a digit.

NOTE

·         If Identifier is start with Underscore(_) then it indicates it is Private.

·         If Identifier is start with __(2 underscores) indicating strongly private.

·         If the identifier starts and ends with two underscore symbols then the identifier is language defined special name, which is also known as magic words.

Reserve Words

In python some words are reserved to represent some meaning or functionality.

There are 33 reserved words in python

·         True, False, None

·         And, or, not, is

·         If, elseif, else

·         While, for, break, continue, return, in, yield

·         Try, except, finally, raise, assert

·         Import, from, as, class, def, pass, global, nonlocal, lambda, del, with

 

DataTypes

Datatype represents the type of data present inside a variable.

In python we are not required to specify the type exclicitly. Based on the value provided, the type will be assigned automatically.

Python contains the following inbuilt datatypes

·         Int

·         Float

·         Complex

·         Bool

·         Byte

·         Str

·         Bytearray

·         Range

·         List

·         Tuple

·         Set

·         Frozenset

·         Dict

·         None

 

Slicing Strings

[] operater is called Slice operator.

In python String index can be +ve or -ve

 

Note

In python, we can represent char values also by using str type and explicitly char type is not available.

 

Type casting

We can convert one type to another type. This conversion is called Type conversion.

Note

·         We can convert from any type to int type except complex type.

·         If we want to convert str type to int type, compulsory str should contain only integral value and should be specified in base 10.

All Fundamental data types are immutable. Once we create an object, we cannot perform any changes in that object. If we are trying to change then with those changes a new object will be created. This is called immutability.

 

List Data type

If we want to represent a group of values as a single entity where insertion order required to preserve and duplicates are allowed then we should go for list data type.

·         Insertion order is preserved.

·         Heterogeneous objects are allowed

·         Duplicates are allowed.

·         Growable in nature.

·         mutable

·         Values should be enclosed within square brackets.

List is growable in nature, based on our requirement we can increase or decrease the size.

Tuple data type

It is exactly same as list data type except that is immutable. We cannot change values.

Tuple elements can be represented in parenthesis.

Note

Tuple is read only version of list

 

Set data type

·         Insertion order is not preserved

·         Duplicates are not allowed

·         Heterogeneous objects are allowed

·         Index concept is not applicable

·         It is mutable collection

·         Growable in nature.

 

Frosenset datatype

·         It is exactly same as set except that is immutable.

·         Hence we cannot use add or remove functions.

 

Dict datatype

·         If we represent a group of values as key value pairs then we should go dict data type.

·         Duplicates keys are not allowed.

 

NOTE

Dict is mutable and the order won’t be preserved.

 

Escape characters

·         /n – new line

·         /t – Horizontal tab

·         /r – Carriage return

·         /b – backspace

·         /f – form feed

·         /v – vertical tab

Constants

·         Constants concept is not applicable in python

·         But it is convention to use uppercase characters if we don’t want to change value.

·         It is just convention but we can change value.

 

Arithmetic operators

2. relational Operators

3. bitwise operators

 

4. Assignment Operators

 

 

Special Operators

·         Identity operators

·         Membership operators

Identity Operators

·         We can use for addresses comparison

·         Operators are – is, is not

NOTE

We can use is operator for addresses comparision where as == operator for content comparision.

Membership operators

·         Used to check whether the given object present in the given collection.

·         Operators – in, not in

 

Operator Precedence

 

Mathematical Functions(math module)

Math is a module that contains several functions to perform mathematical operators

Input()

Input() function can be used to read data directly in our required format. We are not required to perform type casting.

How to read mulptiple values from the keyboard in a single line:

Write a program to read 3 float numbers and their sum

Eval()

Eval function take a string and evaluate the result.

 

NOTE

Eval() can evaluate the input to list, tuple, set, etc based on provided input.

 

Write a program to accept list from the keyboard

 

NOTE

There is no switch statement in python.

Write a program to find Biggest of given 3 numbers

 

To print the characters present in string index wise is

Loops with else block

·         Inside loop execution, if break statement not executed, then only else part will be executed.

·         Else means loop without break.

Pass statement

·         It is an empty statement

·         It is null statement

·         It wont do anything

·         It is like abstract method in java.

 

Del statement

We can delete variable by using del keyword.

 

 

 

 

 

Popular Post

MindMaps

Featured post

Question 1: Reverse Words in a String III

  def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)