Python

Python

Getting Started with Python

Python language was developed by Guido Van Russom in Feb 1991.

It is based on 2 programming languages i-e ABC & Modula-3.

Advantages of python —

  1. Easy to Learn — Python is very programmer friendly & powerful object oriented language (OOR) with very simple syntax rules.
  2. Expressive Language — Its capable of expressing the code purposes than other languages.
  3. Easy to debug — Python is an interpreted language which makes it easy to debug.
  4. Free and open source — Python is freely available from https://python.org
  5. Multiple Assignments – Python is very versatile with assignments

Useful in many fields and applications such as scripting, developing web applications, gaming, system administration, GUI programming, Database application, Rapid prototyping.

Disadvantages of python —

  1. Lack of speed — Less Not the fastest language.
  2. Lack of convertibility — Not easily convertible in another programming language.
  3. Lesser libraries — It has lesser libraries than java.

Python Fundamentals

Token (Lexical Unit)

Python has the following tokens –

  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators
  5. Punctuators

Variable Definition

In Python, a variable is created when you first assign a value to it. It means a variable isn’t created until some value is assigned to it.

 I-e

print(a) — > {error} — > {a isn’t defined}

& if

A = 10

print(A)

it’ll give an output i-e 10.

Interactively Input

Input method is used for interactively manner.

For example   : a = input(“Input A”)

By default this input method returns a value of string type.

For accepting numerical value, Python offers 2 functions : int(), float()

   Data Handling In Python

Data-types

Python offers 5 types of data-types

  1. Numbers
  2. Strings
  3. List
  4. Tuple
  5. Dictionary

Variable Internals

Since Python is an OOR language, it has an object and that object has certain properties and their behaviours.

There are 3 attributes of variable internals in python :

  1. type : It determines the data type of an object.

For Example –   a = 5

              type(a)

               int

  1. value : It’s the data item contained in the object.

For example —  p = 5

              print(p)

              [5]

  1. id : It is the memory location of an object.

For Example – a = 4

            id(a)

            100110       [Could be anything]

Conversion

  1. Implicit Type Conversion – Implicit type conversion method as performed by compilers without programmers intervention to convert a data into another data type.

For Example – a = 5

            b = 1.5

                 b = a

                 print(b)

                 [5.0]

  • Explicit Type Conversion – It is an user defined conversion that forces an expression to be a specific type.

For Example – a = 5

            b = 1.4

            a = int(b)

            print(a)

             [1]

There are many types of conversion function such as int, float, complex, str, bool etc.

String Operators :

  1. Basic Operator – There are 2 types of basic operators : –
  2. +

For Example – “My” + “file”

              [Myfile]

  • *

       For Example – “My”*3

                        [MyMyMy]

  1. Membership Operator – There are 2 types : –
  2.  in – it returns true if a character is present in the given string.

For Example – “In” in “India”

               [true]

  •  not In – it returns true if a sub-string doesn’t exist in  given string.

For Example – “C2” not in “India”

               [true]

  1. Comparison Operator – All relational operators are used for comparing the string.

          For Example –

      A == “A”

      [true]

      “A” != a

      [true]

      “ABC” != “abc”

      [true]

  • ord function – This function returns ordinal value/ASCII value of given character.

  For Example –

  ord(7)

  [90]

  • chr function – This function returns a character according to the entered number.

For Example –

chr(65)

String Slice

Slice means a part of the string.

For Example –

name = “Python”

name[::-2]

nhy

name[1:3:2]

to

name[::-1]

nohtyp

name[:3]

Pyt

  • Syntax

x[starting index : last position index : no of characters]

Conditional Statements in Python

if ­­……..elif……..else statement

Sometimes, you have multiple conditions to execute, then if….elif….else statement is used.

  • Syntax

if condition :

    _________

elif :

    _________

.

.

.

else : _________

Nested If statement

An if in another if’s body is known as a nested if.

  • Syntax

if condition :

if condition :

    _________

else :

    _________

elif condition :

    _________

else :

    _________

Looping Statements in Python

Python provides 2 kinds of loop :

  1. for loop (counting loop) : This loop repeats itself certain number of times in any sequence.

For example –

for i in [1,4,7] :

  print(i)

  print(i*i)

  • while loop : It’s a conditional loop that will repeat the instructions within itself as long as the condition stays true. Its also an example of entry control loop since it checks the condition at the entry point.
  • Syntax

while condition :

   body of loop

   update statement

Infinite Loop : This loop executes endless number of times.

For Example –

a = 1

while a<5 :

print(“India”)

Nested Loop : A loop may contain another loop in its body, it is known as nested loop. Inner loop must terminate before the outer loop.

For Example –

for I in range(1,6) :

   for j in range(1.i) :

      print(“A”)

   print()

List Manipulation In Python

Nested List

List within another list is known as nested list.

For Example –

L1 = [1,2,[3,4],5]

List functions and methods

  1. index – It returns the correct index value.

For Example –

L = [10,20,30,40]

L.index(30)

[2]

  • append – It adds an element at the end of list.

For Example –

color = [‘red’,’pink’,’green’]

color.append(‘Blue’)

[‘red’,’pink’,’green’,’blue’]

  • extend – It is used for adding multiple elements at the end of list like append but append adds only 1 element whereas extend adds multiple elements.

For Example –

L1 = [1,2,3]

L2 = [4,5,6]

L1.extend(L2)

L1 = [1,2,3,4,5,6]

L1 =  [‘a’,’b’,’c’]

  • insert – It is used to insert values on the list.

For Example –

L1.insert(2,’m’)

L1 = [‘a’,’b’,’m’,’c’]

  • remove – It is used to delete an element from a list.

For Example –

L1.remove(‘m’)

L1 = [‘a’,’b’,’c’]

  • clear – It is used to remove all elements of a list.

For Example –

L1.clear()

L1

  • count – It returns the occurrence of items in the list that is given as argument.

For Example –

L1.count(m)

  • reverse – It is used to return a complete list in return form.

For Example –

L1.reverse()

[‘c’,’b’,’a’]

  • sort – It arranges the elements in a particular order.

For Example –

L1.sort(reverse=true)

L1[c,b,a]

  1. pop – It deletes an element from a list with a particular index number given.

For Example –

L1.pop(2)

L1[‘a’,’b’]   

Science & Technology, Technology training , , ,

Discover more from Logic Searcher

Subscribe now to keep reading and get access to the full archive.

Continue reading