Chapter - 3 Python programming fundamentals
Introduction A Python program is called a script. Script is a sequence of definitions and commands. These commands are executed by Python interpreter known as PYTHON SHELL. In python programming,  data types are inbuilt hence support “dynamic typing”  declaration of variables is not required.  memory management is automatically done.
Variables A variable is like a container that stores values that you can access or change. A variable has three main components: a)Identity of variable b)Type of variable c)Value of variable
Variables a) Identity of the variable It refers to object’s address in the memory. >>> x=10 e.g. >>> id (x)
Data Type b) Type of the variable Type means data type of a variable. 1. Number: It stores numerical values. Python supports three built in numeric types – integer, floating point numbers and complex numbers.
Data Type a. int (integer): Integer represents whole numbers. (positive or negative) e.g. -6, 0, 23466 b. float (floating point numbers): It represents numbers with decimal point. e.g. -43.2, 6.0 c. Complex numbers: It is made up of pair of real and imaginary number. e.g. 2+5j
Data Type 2. String (str): It is a sequence of characters. (combination of letters, numbers and symbols). It is enclosed within single or double quotes. e.g. (‘ ’ or “ ”) >>> rem= “Hello Welcome to Python” >>> print (rem) Hello Welcome to Python
Data Type 3. Boolean (bool): It represents one of the two possible values – True or False. >>> bool_1 = (6>10) >>> print (bool_1) False >>> bool_2 = (6<10) >>> print (bool_2) True
Data Type 4. None: It is a special data type with single value. It is used to signify absence of value evaluating to false in a situation. >>> value_1 = None >>> print (value_1) None
type() – if you wish to determine type of the variable. e.g. >>> type(10) <class ‘int’> >>> type(8.2) <class ‘float’> >>> type(“hello”) <class ‘str’> >>> type(True) <class ‘bool’> Data Type
Value c) Value of variable Values are assigned to a variable using assignment operator (=).. e.g. >>>marks=87 >>>print(marks) 87 marks – Name of the variable Value of the variable 87
Value The concept of assignment: There should be only one variable on the left-hand side of assignment operator. This variable is called Left value or L-value There can be any valid expression on the right-hand side of assignment operator. This variable is called Right value or R-value L-value = R-value Value_1 = 100 Variable Assignment operator Value
Value Multiple assignments: We can declare multiple variables in a single statement. e.g. >>>x, y, z, p = 2, 40, 30.5, ‘Vinay’
Variable Naming Convention 1. A variable name can contain letter, digits and underscore (_). No other characters are allowed. 2. A variable name must start with an alphabet or and underscore (_). 3. A variable name cannot contain spaces. 4. Keyword cannot be used as a variable name. 5. Variable names are case sensitive. Num and num are different. 6. Variable names should be short and meaningful. Invalid variable names – 3dgraph, roll#no, first name, d.o.b, while
Input/Output Python provides three functions for getting user’s input. 1. input() function– It is used to get data in script mode. The input() function takes string as an argument. It always returns a value of string type.
Input/Output 2. int() function– It is used to convert input string value to numeric value. 3. float() function – It converts fetched value in float type. 4. eval() – This function is used to evaluate the value of a string. It takes input as string and evaluates this string as number and return numeric result. NOTE : input() function always enter string value in python 3. So on need int(), float() function can be used for data conversion.
Sample Program n1 = input("Enter first number") n2 = input("Enter second number") Sum=n1+n2 print("Sum is:", Sum)
Sample Program n1 = eval(input("Enter first number")) n2 = eval(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum) n1 = int(input("Enter first number")) n2 = int(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum)
Sample Program n1 = eval(input("Enter first number")) n2 = eval(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum) n1 = int(input("Enter first number")) n2 = int(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum)
To calculate simple interest p = int(input("Enter Principal:")) r = float(input("Enter Rate:")) t = int(input("Enter Time:")) si=0.0 si=(p*r*t)/100 print("Simple Interest is:", si)
m1 = int(input("Enter marks in English:")) m2 = int(input("Enter marks in Hindi:")) m3 = int(input("Enter marks in Maths:")) m4 = int(input("Enter marks in Science:")) m5 = int(input("Enter marks in Social Science:")) total=m1+m2+m3+m4+m5 per = total/5 print("Total is:", total) print("Percenatge is:", per) To calculate total and percentage
Python Character Set A set of valid characters recognized by python. • Python uses the traditional ASCII character set. • The latest version recognizes the Unicode character set. The ASCII character set is a subset of the Unicode character set. Letters: a-z, A-Z Digits : 0-9 Special symbols : Special symbol available over keyboard White spaces: blank space, tab, carriage return, new line, form feed Other characters: Unicode
Tokens Smallest individual unit in a program is known as token. 1. Keywords 2. Identifiers 3. Literals 4. Operators 5. Delimiters
1. Keywords Reserved word of the compiler/interpreter which can’t be used as identifier.
2. Identifiers A Python identifier is a name used to identify a variable, function, class, module or other object.
3. Literals Literals in Python can be defined as number, text, or other data that represent values to be stored in variables. Example of String Literals in Python name = ‘Johni’ , fname=“johny” Example of Integer Literals in Python(numeric literal) age = 22 Example of Float Literals in Python(numeric literal) height = 6.2 Example of Special Literals in Python name = None
3. Literals Escape sequence e.g. print(“I am a student of n APS t Yol Cantt”)
4. Operators Operators can be defined as symbols that are used to perform operations on operands. Types of Operators a. Arithmetic Operators. b. Relational Operators. c. Assignment Operators. d. Logical Operators. e. Membership Operators f. Identity Operators
4. Operators a. Arithmetic Operators. Arithmetic Operators are used to perform arithmetic operations like addition, multiplication, division etc.
print ("hello"+"python") print(2+3) print(10-3) print(22%5) print(19//5) print(2**3) Output: hellopython 5 7 2 3 8
4. Operators b. Relational Operators. Relational Operators are used to compare the values.
print(5==3) False print(7>3) True print(15<7) False print(3!=2) True print(7>=8) False print(3<=4) True
4. Operators c. Assignment Operators. Used to assign values to the variables.
a=10 print(a) a+=9 print(a) b=11 b*=3 print(b) c=19 c/=2 print(c) d=2 d**=4 print(d) Output: 10 19 33 9.5 16
4. Operators d. Logical Operators. Logical Operators are used to perform logical operations on the given two variables or values. a=30 b=20 if(a==30 and b==20): print("hello") Output :- hello a=70 b=20 if(a==30 or b==20): print("hello")
4. Operators e. Membership Operators It used to validate whether a value is found within a sequence such as such as strings, lists, or tuples. E.g. a = 22 list = [11, 22,33,44] ans= a in list print(ans) Output: True E.g. a = 22 list = [11, 22,33,44] ans= a not in list print(ans) Output: False
4. Operators f. Identity Operators Identity operators in Python compare the memory locations of two objects.
5. Delimiters These are the symbols which can be used as separator of values or to enclose some values. e.g ( ) { } [ ] , ; :
Token Category X Variable y Variable z Variable print Keyword ( ) Delimiter / Operator 68 Literal “x, y, z” Literal
Comments Comments are statements in the script that are ignored by the Python interpreter. Comments (hash symbol = #) makes code more readable and understandable. # Program to assign value x=10 x=x+100 # increase value of x by 100 print(x)
Expressions Expressions are combination of value(s). i.e. constant, variable and operators. Expression Value 5+2*4 13 8+12*2-4 28 Converting mathematical expression to equivalent Python expression Algebraic Expression Python Expression y = 3 ( 𝑥 2 ) y = 3 * x / 2 z= 3bc + 4 z = 3*b*c + 4
User Defined Functions A function is a group of statements that exists within a program for the purpose of performing a specific task. Syntax: def function_name(comma_sep_list_parameters): statements
User Defined Functions Example 1: def display(): print(“Welcome to pyhton”) >>>display() Example 2: def arearec(len, wd): area=len*wd return area >>>arearec(30, 10)

IMP PPT- Python programming fundamentals.pptx

  • 1.
    Chapter - 3 Pythonprogramming fundamentals
  • 2.
    Introduction A Python programis called a script. Script is a sequence of definitions and commands. These commands are executed by Python interpreter known as PYTHON SHELL. In python programming,  data types are inbuilt hence support “dynamic typing”  declaration of variables is not required.  memory management is automatically done.
  • 3.
    Variables A variable islike a container that stores values that you can access or change. A variable has three main components: a)Identity of variable b)Type of variable c)Value of variable
  • 4.
    Variables a) Identity ofthe variable It refers to object’s address in the memory. >>> x=10 e.g. >>> id (x)
  • 5.
    Data Type b) Typeof the variable Type means data type of a variable. 1. Number: It stores numerical values. Python supports three built in numeric types – integer, floating point numbers and complex numbers.
  • 6.
    Data Type a. int(integer): Integer represents whole numbers. (positive or negative) e.g. -6, 0, 23466 b. float (floating point numbers): It represents numbers with decimal point. e.g. -43.2, 6.0 c. Complex numbers: It is made up of pair of real and imaginary number. e.g. 2+5j
  • 7.
    Data Type 2. String(str): It is a sequence of characters. (combination of letters, numbers and symbols). It is enclosed within single or double quotes. e.g. (‘ ’ or “ ”) >>> rem= “Hello Welcome to Python” >>> print (rem) Hello Welcome to Python
  • 8.
    Data Type 3. Boolean(bool): It represents one of the two possible values – True or False. >>> bool_1 = (6>10) >>> print (bool_1) False >>> bool_2 = (6<10) >>> print (bool_2) True
  • 9.
    Data Type 4. None:It is a special data type with single value. It is used to signify absence of value evaluating to false in a situation. >>> value_1 = None >>> print (value_1) None
  • 10.
    type() – ifyou wish to determine type of the variable. e.g. >>> type(10) <class ‘int’> >>> type(8.2) <class ‘float’> >>> type(“hello”) <class ‘str’> >>> type(True) <class ‘bool’> Data Type
  • 11.
    Value c) Value ofvariable Values are assigned to a variable using assignment operator (=).. e.g. >>>marks=87 >>>print(marks) 87 marks – Name of the variable Value of the variable 87
  • 12.
    Value The concept ofassignment: There should be only one variable on the left-hand side of assignment operator. This variable is called Left value or L-value There can be any valid expression on the right-hand side of assignment operator. This variable is called Right value or R-value L-value = R-value Value_1 = 100 Variable Assignment operator Value
  • 13.
    Value Multiple assignments: We candeclare multiple variables in a single statement. e.g. >>>x, y, z, p = 2, 40, 30.5, ‘Vinay’
  • 14.
    Variable Naming Convention 1.A variable name can contain letter, digits and underscore (_). No other characters are allowed. 2. A variable name must start with an alphabet or and underscore (_). 3. A variable name cannot contain spaces. 4. Keyword cannot be used as a variable name. 5. Variable names are case sensitive. Num and num are different. 6. Variable names should be short and meaningful. Invalid variable names – 3dgraph, roll#no, first name, d.o.b, while
  • 15.
    Input/Output Python provides threefunctions for getting user’s input. 1. input() function– It is used to get data in script mode. The input() function takes string as an argument. It always returns a value of string type.
  • 16.
    Input/Output 2. int() function–It is used to convert input string value to numeric value. 3. float() function – It converts fetched value in float type. 4. eval() – This function is used to evaluate the value of a string. It takes input as string and evaluates this string as number and return numeric result. NOTE : input() function always enter string value in python 3. So on need int(), float() function can be used for data conversion.
  • 17.
    Sample Program n1 =input("Enter first number") n2 = input("Enter second number") Sum=n1+n2 print("Sum is:", Sum)
  • 18.
    Sample Program n1 =eval(input("Enter first number")) n2 = eval(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum) n1 = int(input("Enter first number")) n2 = int(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum)
  • 19.
    Sample Program n1 =eval(input("Enter first number")) n2 = eval(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum) n1 = int(input("Enter first number")) n2 = int(input("Enter second number")) Sum=n1+n2 print("Sum is:", Sum)
  • 20.
    To calculate simpleinterest p = int(input("Enter Principal:")) r = float(input("Enter Rate:")) t = int(input("Enter Time:")) si=0.0 si=(p*r*t)/100 print("Simple Interest is:", si)
  • 21.
    m1 = int(input("Entermarks in English:")) m2 = int(input("Enter marks in Hindi:")) m3 = int(input("Enter marks in Maths:")) m4 = int(input("Enter marks in Science:")) m5 = int(input("Enter marks in Social Science:")) total=m1+m2+m3+m4+m5 per = total/5 print("Total is:", total) print("Percenatge is:", per) To calculate total and percentage
  • 22.
    Python Character Set Aset of valid characters recognized by python. • Python uses the traditional ASCII character set. • The latest version recognizes the Unicode character set. The ASCII character set is a subset of the Unicode character set. Letters: a-z, A-Z Digits : 0-9 Special symbols : Special symbol available over keyboard White spaces: blank space, tab, carriage return, new line, form feed Other characters: Unicode
  • 23.
    Tokens Smallest individual unitin a program is known as token. 1. Keywords 2. Identifiers 3. Literals 4. Operators 5. Delimiters
  • 24.
    1. Keywords Reserved wordof the compiler/interpreter which can’t be used as identifier.
  • 25.
    2. Identifiers A Pythonidentifier is a name used to identify a variable, function, class, module or other object.
  • 26.
    3. Literals Literals inPython can be defined as number, text, or other data that represent values to be stored in variables. Example of String Literals in Python name = ‘Johni’ , fname=“johny” Example of Integer Literals in Python(numeric literal) age = 22 Example of Float Literals in Python(numeric literal) height = 6.2 Example of Special Literals in Python name = None
  • 27.
    3. Literals Escape sequence e.g. print(“Iam a student of n APS t Yol Cantt”)
  • 28.
    4. Operators Operators canbe defined as symbols that are used to perform operations on operands. Types of Operators a. Arithmetic Operators. b. Relational Operators. c. Assignment Operators. d. Logical Operators. e. Membership Operators f. Identity Operators
  • 29.
    4. Operators a. ArithmeticOperators. Arithmetic Operators are used to perform arithmetic operations like addition, multiplication, division etc.
  • 30.
  • 31.
    4. Operators b. RelationalOperators. Relational Operators are used to compare the values.
  • 32.
    print(5==3) False print(7>3) True print(15<7)False print(3!=2) True print(7>=8) False print(3<=4) True
  • 33.
    4. Operators c. AssignmentOperators. Used to assign values to the variables.
  • 34.
  • 35.
    4. Operators d. LogicalOperators. Logical Operators are used to perform logical operations on the given two variables or values. a=30 b=20 if(a==30 and b==20): print("hello") Output :- hello a=70 b=20 if(a==30 or b==20): print("hello")
  • 36.
    4. Operators e. MembershipOperators It used to validate whether a value is found within a sequence such as such as strings, lists, or tuples. E.g. a = 22 list = [11, 22,33,44] ans= a in list print(ans) Output: True E.g. a = 22 list = [11, 22,33,44] ans= a not in list print(ans) Output: False
  • 37.
    4. Operators f. IdentityOperators Identity operators in Python compare the memory locations of two objects.
  • 39.
    5. Delimiters These arethe symbols which can be used as separator of values or to enclose some values. e.g ( ) { } [ ] , ; :
  • 40.
    Token Category X Variable yVariable z Variable print Keyword ( ) Delimiter / Operator 68 Literal “x, y, z” Literal
  • 41.
    Comments Comments are statementsin the script that are ignored by the Python interpreter. Comments (hash symbol = #) makes code more readable and understandable. # Program to assign value x=10 x=x+100 # increase value of x by 100 print(x)
  • 42.
    Expressions Expressions are combinationof value(s). i.e. constant, variable and operators. Expression Value 5+2*4 13 8+12*2-4 28 Converting mathematical expression to equivalent Python expression Algebraic Expression Python Expression y = 3 ( 𝑥 2 ) y = 3 * x / 2 z= 3bc + 4 z = 3*b*c + 4
  • 43.
    User Defined Functions Afunction is a group of statements that exists within a program for the purpose of performing a specific task. Syntax: def function_name(comma_sep_list_parameters): statements
  • 44.
    User Defined Functions Example1: def display(): print(“Welcome to pyhton”) >>>display() Example 2: def arearec(len, wd): area=len*wd return area >>>arearec(30, 10)