Showing posts with label Numeric data types. Show all posts
Showing posts with label Numeric data types. Show all posts

9/7/22

Python data types and string

Python Notes Unit 1, Part 2

UNIT – 1(part 2)

     




Introduction to Python Part 1

Introduction to Python Part 3

Introduction to Python

History and its Features

Basic Syntax

Understanding Python variables

Numeric data types

Using string data type and string operations

Basic Operators

Understanding coding blocks.

Defining list and list slicing,

Other Data Types- Tuples. List, Python Dictionary, Arrays, Associative Arrays/Hashes


 

Data types: -

    1.   The type of variable present in our code is represented by Data Types.

    2.   A data type is the classification of data items.

    3.   Data type represents the kind of value that tell what operation can perform on particular code.

    4.   Various type of data types that define the storage method on each of them.

 

Numeric data type

In python, three types of numeric data types

1.   Integer

2.   Float

3.   Complex number

Mainly two types of numeric data types are used in code.

                    Integer or float.

 Numeric data type represents the data that has a numeric value. This numeric value can be integer, float and complex number. These values are defined as int, float and complex classes.

 

For example – int data type

                         a=5

  print (“the type of a”, type(a))

  Output – The type of a <class ‘int’ >

 

For example – float datatype

            a = 4.5

            print (“the type of b”, type(b))

Output – The type of b <class ‘float’ >

 

 

Dictionary data type –

    1.   Dictionary data type are working as hash table type.

    2.   In dictionary data type each key are stored and indicate different value at searching time.

Boolean data type: -

    1.   Boolean data type is also a type of one of the built-in data types.

    2.   This data type is representing one of the two values.

a.    Either TRUE

b.   Or FALSE

    3.   Boolean data type is also a logical data type than can have only the values.

    4.   True and False are also used as keywords.

    5.   The keyword True and False must an Upper Case first letter if we are using lowercase true return an error.

Example

               print(True)

               print(type(True))

               print(False)

               print(type(False))

Output

            True

            <class ‘bool’>

            False

            <class ‘bool’>

 

Set data type: -

    1.   A set data type is collection which is unordered, unchangeable and unindexed.

    2.   Set is defined by comma inside braces { }.

    3.   Items under the set are not ordered.

Example –

                   a = {5, 2, 3, 1, 4}

                   print (“a = “, a)

                   print(type(a))

Output -  

              a= {1, 2, 3, 4, 5}

              <class ‘set’>

                 

Sequence type data type   -

1.   Sequence type data type is used to store data in containers.

2.   List, Tuple and String are the different types of containers in python programming language.




 

String: -

    1.   A string is generally considered as a data type and is often implemented as an array data structure of bytes that stores a sequence of elements, typically characters, using some character encoding.

    2.   Strings are sequences of character data.

    3.   The string type in Python is called str.

    4.      String can be considered as a special type of sequence, where all its elements are characters.

 

For example- string “Hello, World” is basically in sequence

 [‘H’,’E’,’L’,’L’,’O’,’’,, ‘ ’,’W’,’O’,’R’,’l’,’d’]

 

Declaration of strings: - 

                        >>> mystring = “This is not my first copy”

                         >>> print (mystring);

                         This is not my first copy            

List –

    1.   The list data type is a versatile data type.

    2.   In Python is list can simultaneously hold different types of data.

    3.   Formally list is an ordered sequence.

    4.   Defining a list in python is easy – just use the brackets syntax to indicate items in a list.

               List_of_ints= [1, 2, 3]

    5.   Items in a list do not have to all be the same type, either. They can be any python object.

 

Tuple –

    1.   The tuple data type is the same as List Datatype except that it is immutable, that means once we create any tuple, you cannot make any changes to that.

    2.   The tuple is a Read-only version of List, which means we cannot add, remove and replace any elements.

 The syntax for the Tuple is –

                                    Tuple=(Elements)

 

Example-          

                              Tuple= (10,20,30, “banana”)

                               Print(type(Tuple))

                         

Output - <class ‘tuple’>

 

List Slicing          

    1.   List Slicing refers to accessing a specific portion of a subset of the list for some operation while the original list remains unaffected.

    2.   The slicing operator in python can take three parameters out of which two are optional depending on the requirement.

 

Syntax of list slicing:    

                              List_name [start:stop:steps]          

    3.   The start parameter is a mandatory parameter, whereas the stop and steps are both optional parameters.

                       

    a.    The start represents the index from where the list slicing is supposed to begin. Its default value is 0, it begins from index 0.

    b.   The stop represents the last index up to which the list slicing will go on. Its default value is (length(list)-1) or the index of last element in the list.

    c.    The step represents the number of steps, i.e after every n number of steps, the start index is updated and list slicing is performed on that index, or in simple words, steps if defined, specifies the number of elements to jump over while counting from start to stop.

 

    This means we can do list slicing in three ways:

    1.   By passing just the start or stop parameter

    2.   By passing the start and stop parameter.

    3.   By passing the start, stop and steps parameters.

 




Next Part is coming soon..!! keep study and keep growing

Thank you..!!






9/1/22

Introduction to Python Programming

 Python Notes Unit 1, Part 1

UNIT – 1(part 1)

    


   Introduction to Python Part 2

  Introduction to Python Part 3 


Introduction to Python

History and its Features

Basic Syntax

Understanding Python variables

Numeric data types

Using string data type and string operations

Basic Operators

Understanding coding blocks.

Defining list and list slicing,

Other Data Types- Tuples. List, Python Dictionary, Arrays, Associative Arrays/Hashes

 

Introduction to Python

1.     Python is a general-purpose high-level programming language was introduced by Guido Van Rossum, in 1991.

2.     Python is also dynamic and interpreted language.

3.     It uses new lines to complete a command, as opposed to other programming language. Python cannot use any semicolons and parenthesis.

4.     Python programmers are often known as Pythonists or Pythonistas.

5.     This language is easy and understandable for learners.

 

History of Python: -

    1.   In 1980s Python laid its foundation and its implementation was started in December 1989 by Guido Van Rossum at CWI in Netherland.

    2.   The code was published by Guido Van Rossum in February 1991 with labeled version 0.9.0 to alternate sources.

    3.   In the year 1994, Python 1.0 was released with its new and advance features like lambda, map, filter, and reduce.

    4.   After that Python 2.0 were published with some more features like list comprehensions, garbage collection systems etc.

    5.   On 3rd December 2008 Python 3.0 also know as “Py3k” was released.

 

Features of Python: -

 These are following features of python:-

1.   Simple and Easy to learn.

2.    High Level Programming Language

3.   Platform independent

4.   Portability

5.   Dynamically Typed

6.   Procedure oriented and object oriented                               

Simple and Easy to learn: -

                       i.         Python code looks like English words.

                     ii.         There is no use of semicolons and brackets, and the indentation of the code block.

                   iii.         We can tell what the code is supposed to do simply by looking at it.

High Level Programming Language: -

                     i.           Programmers don’t need to remember the system architecture, and not do they have to manage the memory.

                   ii.           High level programming approach makes it super programmer-friendly and is one of the key features of Python.

Platform Independent: -

                       i.         Python programs are platform independent.

                     ii.         Once we write a program, it can run on any platform without rewriting once again.

Portability: -

                       i.         Python is portable in the sense that the same code can be used on different machines.

                     ii.          Suppose we are writing a python program on Mac OS and if we want to run it on Windows and Linux later, we don’t have to make any changes in the program.

Procedure oriented and Object oriented: -

                       i.         A programming language is procedure-oriented if it focuses on functions.

                     ii.         A programming language is object-oriented if it focuses on design around data and objects, rather than functions and logic.

                   iii.         Python features is that it supports both procedure-oriented and object-oriented.

 

Basic Syntax: -

Syntax is one of the basic requirements that we must identify to code in any languages.

In Python, we use indentations (the action of indenting) to represent the block of the code.

Let’s understand basic syntax of Python Programming with the help of some line of codes: -

                                        print (‘id:’,1)

                                        print (‘name:’, ‘Diploma Student’)

                                        print (‘age:’, ‘18’)

Python programming language don’t use any terminator operator which means we don’t have to use any semicolon symbol or any bracket to terminate the code.

If we have to print something we can use below code: -

               print (“Diploma Student..!!”)

 

Here is one single statement which is used to print “Diploma Student..!!

 

 

Python Variables: -

    1.   Python variable is containers which store values.

    2.   In python there is no need to define type of a variable hence python is called dynamically-typed language.

A=25         #type of a is int

B=25.4      #type of b is float

C= ‘hello’ #type of c is str

    3.   A variable is created at that the moment when we first assign a value to it.

    4.   A python variable is a name given to a memory location.

    5.   Python variable is the basic unit of storage in a program.

    6.   Type of variable can check using built in function type()

Name= ‘Diploma student’

Print(type(name))     #type will be reported as str

    7.   Simple variable assignment.

var = “sem6”

print (Var).

Rules of creating variables is python: -

(a)         A variable name must start with a letter (A-Z or a-z) or underscore character.

(b)         A variable name cannot start with a number (1abc is not allowed).

(c)         A variable name can only contain alpha-numeric characters and underscores (A-Z, a-z, 0-9, and _)

(d)         Variables name are case sensitive ( Example- name, Name and NAME are three different variables.).

In python, there are mainly two types of variables: -

1.   Local variables

2.   Global variables

 

Next Part is coming soon..!! keep study and keep growing

Thank you..!!

Latest Update

New Web Site

Popular Posts