Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python Unit - 1

The document provides an overview of Python's origins, features, and basic concepts, including its development history from its creation by Guido van Rossum in the 1980s to its various versions. It covers Python's syntax, variable assignments, identifiers, built-in types, and standard operators, along with guidelines for coding style. Additionally, it explains object-oriented programming concepts such as classes, objects, attributes, and methods.

Uploaded by

SATHISHKUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Unit - 1

The document provides an overview of Python's origins, features, and basic concepts, including its development history from its creation by Guido van Rossum in the 1980s to its various versions. It covers Python's syntax, variable assignments, identifiers, built-in types, and standard operators, along with guidelines for coding style. Additionally, it explains object-oriented programming concepts such as classes, objects, attributes, and methods.

Uploaded by

SATHISHKUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT – 1 ORIGINS

Python – origins – features – variable and assignment –Python Development


basics – statement and syntax – Identifiers –Basic style guidelines –  Developed by Guido van Rossum.
Python objects – Standard types and other built-in types – Internal  Developed in 1980’s.
types – Standard type operators – Standard type - Built-in functions. Python 0.90
 First version
PYTHON BASICS  Released in Feb, 1991
 It was created by Guido van Rossum  Support class. Inheritance, exception, functions & modules
 Released in 1991 Python 1.0
 Case Sensitive  Released in Jan, 1994.
 High-level language.  Support map & filer etc.
 Interpreted. Python 2.x
 Object-oriented.  Released in Oct, 2000.
 Open source.  Support more features than previous.
 Cross-Platform. Python 3.x

FEATURES  Released in Dec, 2008.

 Build websites.  It is also known as 3000 or Py3K.

 Software Development. Python 3.5+


 New release called Release Cycle with a Long-term Support
 Machine Learning.
(LTS).
 Artificial Intelligence.
 Developed in 2015.
 Data Science.
Python 3.8 and beyond
 Automate tasks.
 Released in Oct, 2019.
 Analyze data.
 Included many features.
 Python 3.9 & 3.10 Continued with additional features.

V. Naresh Kumar, M.C.A., M.Phil., Page 1


PYTHON BASICS Playground section
 Create an object of the class.(naresh)
Simple Example Program:
# Sample Python program.
import math
s = int(input("enter the value:"))
print (math.sqrt(s))
Output:
Enter the value: 25
5

Documentation section:
 Mention the program name.
 Optional.
Import statements
 Includes various in-built or user-defined modules.
Global declaration section
 Variable that we can access from anywhere in the program
Class section
 Information about the user-defined classes.
Subprogram section
 Set of statements that will execute.

V. Naresh Kumar, M.C.A., M.Phil., Page 2


STATEMENT 2. Multi-Line Statements

 The source code executed by the Python interpreter is called  (\n) used for the next line.

a statement.  But, also separated by continuation character (\).

 The Python has different types of statements like, Example:

1. Assignment Statements Program

2. Conditional Statements
3. Looping Statements, Etc.,
1. Single Line Statements:
 # used for comment.
 Separated by semicolons (;)
Output
Example:
Program

Output

V. Naresh Kumar, M.C.A., M.Phil., Page 3


IDENTIFIERS VARIABLE AND ASSIGNMENT
 Naming the variable, class and function. Variables
Rules for Naming an Identifier  Variables can be defined as containers.
 Identifiers cannot be a keyword. Rules for Variable declaring
 Identifiers are case-sensitive.  Cannot be a keyword.
 It can have a sequence of letters and digits. ...  Start with an alphabetic (Or) Underscore.
 Start an identifier with a letter (Or) Underscore.  Whitespaces are not allowed.
 Whitespaces are not allowed.  We cannot use special symbols like !, @, #, $, and so on.
 We cannot use special symbols like !, @, #, $, and so on. Variable Assignments
 Example:  There are two types of variable assignments,
1. Single value assignment:
 Variables that store only one data value.
Example:

2. Multiple Value Assignment:


 Variables that store more than one data value.

V. Naresh Kumar, M.C.A., M.Phil., Page 4


BASIC STYLE GUIDELINES 5. Using docstrings

1. Use a tab for indentation  Used in functions and classes.

 Use a tab for indentation  Use the triple quotes.

 This makes code more readable for multiple functions and Example:

method. def sample():

Example """This is a function"""

def sample(random): """

# statement 1 This

# statement 2 is

# ... a function

return random """

2. ASCII encoding class Sample:

 Using Pythons' default ASCII encoding is the best practice """This is a class"""

for international environments. """

3. Comma in tuples This

 It's not mandatory is

Example a class

tup = (1, 2, 3,) """

4. Using spaces 6. Comment Updating

 It's best to use a space before and after an operator.  Don't forget to update the comments while updating code.

 Use a space after the comma is more readability.  It's one of the most important things in coding.

Example 7. Character that shouldn't be used as variable names singly

import random  Characters that shouldn't be used as variable names lonely.


result = random.randint(1, 3) + random.randint(1, 2) 8. Don't use ASCII characters in identifiers
 Using ASCII characters in identifiers is not at all a good
practice.

V. Naresh Kumar, M.C.A., M.Phil., Page 5


STANDARD TYPES ii) float
 This contains real numbers.
Example:
b = 40.5
print("The type of b", type(b))
2. Sequence Type
 Python supports three kinds of Sequence type,
o String
o List
o Tuple
String:

1. Numeric  Quotation marks can be used to describe the string.

 Values are stored in numbers.  A string can be defined using


o Single.
 type() - determine the types of variables.
o Double (Or) Triple quotes.
 Python supports three kinds of numerical data.
Example:
o int
str = "string using double quotes"
o float
print(str)
o complex
List
i) int
 List is like arrays in C.
 This contains Whole number
 But, here it contains data of different types.
Example:
a=5  It encased square bracket [].

print("The type of a", type(a)) Example:


str = ["string using double quotes"]
print(str)

V. Naresh Kumar, M.C.A., M.Phil., Page 6


Tuple OTHER BUILT-IN TYPES:
 Tuples is like list. 1. Binary Types:
 This also contains data of different types.  Bytes: Immutable sequence of bytes.
 It encased brackets ( ).  Byte array: Mutable sequence of bytes.
Example: 2. Numeric Types:
str = ("string using double quotes")  Decimal: Represents fixed-point and floating-point
print(str) arithmetic.
3. Dictionary  Fraction: Represents rational numbers.
 A dictionary is a key-value pair set. 3. Iterator Types:
 Elements are arranged in any order.  Iter: Returns an iterator object for an iterable.
 Curly braces { } are used to separate the items. 4. Generator Types:
Example:  Generator: Represents a generator function.
str = {"string using double quotes"} 5. Text Type:
print(str)  str: Besides being a standard type, it's worth mentioning as it
4. Boolean has various methods for text manipulation.
 True and false are the two default values. 6. File Types:
 False can be represented by the 0 or the letter "F,".  File: Represents a file object.
 True can be represented by any value that is not zero. 7. Module Types:
5. Set  Module: Represents a module.
 The data types are in unordered. 8. Function Types:
Example:  Function: Represents a function.
num = {1, 2, 3} 9. Code Types:
 Code: Represents compiled Python code.
10. Type Types:
 Type: Represents the type of an object.

V. Naresh Kumar, M.C.A., M.Phil., Page 7


INTERNAL TYPES 6. dict:
 Internal types are fundamental to Python and are used to  Represents a dictionary, an unordered collection of key-value
represent and manipulate data. pairs.
1. int: Example:
 Represents integer values. my_dict = {'name': 'John', 'age': 30}
Example: 7. set:
x = 42  Represents an unordered collection of unique elements.
2. float: Example:
 Represents floating-point (decimal) values. my_set = {1, 2, 3, 4}
Example: 8. bool:
y = 3.14  Represents boolean values, either True or False.
3. str: Example:
 Represents strings (sequences of characters). flag = True
Example: 9. NoneType:
s = "Hello, World!"  Represents the absence of a value.
4. list: Example:
 Represents ordered, mutable sequences. my_variable = None
Example: 10. Complex:
my_list = [1, 2, 3, 4]  Represents complex numbers.
5. tuple: Example:
 Represents ordered, immutable sequences. complex_num = 3 + 4j
Example:
my_tuple = (1, 2, 3, 4)

V. Naresh Kumar, M.C.A., M.Phil., Page 8


BUILT –IN FUNCTION 6. Logical Operations:

1. Type Conversion: bool():

 Convert between different data types.  Convert a value to a Boolean.

 int(), float(), str(), list(), tuple(), set(), dict() any(), all():

2. Math Functions:  Check if any or all elements in an iterable are true.

 Perform mathematical operations. 7. File Operations:

 abs(), pow(), round(), max(), min()  Perform file operations.

3. Input / Output:  open(), read(), write(), close():

print(): 8. Object-Oriented Programming:

 Print to the console.  Check object types and class relationships.

input():  isinstance(), issubclass():

 Read input from the user. 9. Functional Programming:

4. String Manipulation:  Perform functional programming operations.


len():  map(), filter(), reduce():
 Get the length of a string or collection. 10. Exception Handling:
str(), ord(), chr()  Handle exceptions or Error.
 Convert between strings and Unicode characters.  try, except, finally:
format(): 11. Time and Date:
 Format strings.  Work with time and date.
5. List, Tuple, Set, and Dictionary Operations:  time(), datetime(), strftime()
sorted(), reversed():
 Return sorted or reversed versions of a sequence.
sum(), max(), min():
 Calculate the sum, maximum, or minimum of a sequence.

V. Naresh Kumar, M.C.A., M.Phil., Page 9


STANDARD TYPE OPERATORS 2. isinstance()
1. type()  Check if a variable is of a certain type.
 Determines the type of an object.  function takes two arguments.
Example:

 Additionally, for comparing types, the == operator can be


used.

V. Naresh Kumar, M.C.A., M.Phil., Page 10


PYTHON OBJECTS Inheritance:
Class:  The classes inherit the attributes and methods from another

 It is a user-defined data type. class.


 Defines a blueprint for creating objects.  The class inherited from is parent class.
 It encapsulates data and the methods.  The class inherits is called the child class.
 The parent class is also called base class.
 The child class is also called derived class
Example:

Object:
 An object is an instance of a class.
 It is created from the class.
Attributes:
 Attributes are data members.
 Hold information about the object.
 Defined within the class.
Methods:
 Methods are functions.
 Defined within a class. ------------------------------------- END -------------------------------------

 Perform operations on the object's data.

V. Naresh Kumar, M.C.A., M.Phil., Page 11

You might also like