Más contenido relacionado


Python lab basics

  2. • Python is a high level, interpreted , interactive and object-oriented scripting language. • It is a highly readable language. • Unlike other programming languages, python provides an interactive mode similar to that of a calculator.
  3. PYTHON OVERVIEW • The code written in python is automatically compiled to byte code and executed. • Python can be used as a scripting language. • Features such as nested code blocks, functions, classes, modules and packages. • It make use of object-oriented programming approach.
  4. CONSOLE or COMMAND SHELL The interactive environment where we are interacting with the python interpreter is called the console or command shell.
  5. Practice Now, try to interact with the interpreter by entering a simple expression, 5+2, on the console
  6. Print Statement The print statement is used to display the output to the screen
  7. Practice Now, type the following text at the python prompt Note: If you want to display a message on the console, you will need to keep your message within quotes. Print “Hi Friends, Welcome to Python Laboratory” Or Print ‘Hi Friends, Welcome to Python Laboratory’
  8. COMMENTS • It is used by the programmer to explain the piece of code to others as well as to himself in a simple language. • Python uses the hash character (#) for comments.
  9. Practice >>>5+2 #addition
  10. PYTHON IDENTIFIERS • A python identifier is the name given to a variable, function, class, module or other object. • It can include any number of letters, digits or underscores • Spaces are not allowed • It will not accept @, $,% as identifiers
  11. • Python is a case-sensitive language. • Hello and hello are different identifiers. VALID NOT VALID MyName My Name (Space is not allowed) My_Name 3dig (cannot start with a digit) Your_Name Your#Name (Only alphabetic character, Underscore(_) and numeric are allowed)
  12. VARIABLES 1. Declaring a Variable 2. Initializing a Variable
  13. Declaring a Variable • A variable holds a value that may change. • The process of writing the variable name is called Declaring the variable. • In Python, variables do not need to be declared explicitly in order to reserve memory spaces as in other programming languages. • When we initialize the variable in python, python interpreter automatically does the declaration process.
  14. Initializing a Variable • The general format of assignment statement is as follows: • Equal sign (=) is known as assignment operator. • An expression is any value, text or arithmetic expression • Variable is the name of the variable. • The value of the expression will be stored in the variable Variable = expression
  15. Practice >>> Year=2017 >>> Department = ‘CSE’
  16. Practice >>>Department = ‘CSE’ >>> Dept=Department >>>Dept
  17. Practice >>>Department = ‘CSE’ >>>Department = ‘ECE’ >>>Department
  18. Standard Data Types • The data stored in the memory can be of many types. • Example: PERSON NAME ADDRESS AGE >20 Alphabetic Alphanumeric Yes or No (Boolean)
  19. Data Types Python Data Types Numeric String List Tuple Dictionary Boolean
  20. Numeric • Broadly divided into Integers and Real Numbers (i.e., Fractional Numbers or floating point numbers (Fractional Part and Decimal Part)).
  21. Practice >>>num1=2 #integer number >>> num2=2.5 #real number(float) >>>num1 >>>num2
  22. String • Single quotes or double quotes are used to represent strings • A string in python can be a series or a sequence of alphabets, numerals and special characters. • Similar to C, the first character of a string has an index 0.
  23. • Slice Operator ([] and [:]) –Subset of the string • Concatenation Operator (+) – Combine two or more than two strings • Repetition Operator (*) - Repeat the same string several times
  24. Practice >>> string1=“Hi Friends” # Store String Value >>>string1 # display string value >>> string1 + “How are You” #use of + operator >>> string1 *2 #use of * operator
  25. Practice >>>string1 >>>string1[1] #display 1st index element >>>string1[0:2] #display 0 to 1st index element >>>string1=“HelloWorld” >>>string1[1:8:2] #display all the alternate characters between index 1 to 8
  26. LIST • List is the most used data type in python. • It can contain the same or different type of items. • To declare a list in python->Separate items using commas and enclose them within square brackets([])
  27. Practice >>>first=[1,”two”,3.0,”four”] #1st list >>>second=[“five”,6] #2nd list >>>first >>>first+second # concatenate 1st and 2nd list >>>second*3 #repeat 2nd list >>>first[0:2] #display sublist
  28. Tuple • Similar to a list, tuple is also used to store sequence of items. • Like a list, a tuple consists of items separated by commas. • Tuples are enclosed within parentheses
  29. Practice >>>third=(7,”eight”,9,10.0) >>>third
  30. Difference between tuple and list LIST Tuple Items are enclosed within square brackets[] Items are enclosed within parentheses () Lists are mutable Tuples are immutable
  31. Practice >>>first[0]=“one” >>>third[0]=“seven”
  32. Dictionary • It is same as the hash table type. • The order of elements in a dictionary is undefined • Unordered collection of key-value pairs. • When we have large amount of data, the dictionary data type is used. • Keys and values can be of any type in dictionary
  33. • Items in dictionaries are enclosed in the curly- braces {} • Separated by the comma (,) • A colon is used to separate key from value • A key inside the square bracket [] is used for accessing the dictionary items.
  34. Practice >>>dict1={1:”first”,”second”:2} #declare dictionary >>>dict1[3]=“third line” #add new item >>>dict1 >>>dict1.keys() # display dictionary keys >>>dict1.values() # display dictionary values
  35. Boolean The True and False data is known as Boolean Data and data types which stores this boolean data known as Boolean Data types
  36. Practice >>>a=TRUE >>>type(a) >>>b=FALSE >>>type(b)
  37. SETS • It is an unordered collection of data known as set. • It does not contain any duplicate values or elements. • Union, Intersection, Difference and Symmetric Difference  Operations which are performed on sets
  38. • Union (|): It performs on two sets returns all the elements from both the sets. • Intersection (&) : It performs on two sets returns all the elements which are common in both the sets • Difference (-): It performs on two sets set1 and set2 returns the elements which are present in set1 but not in set2. • Symmetric Difference (^): It performs on two sets returns the element which are present in either set1 or set2 but not in both.
  39. Practice #Defining sets >>>grp1=set([1,2,4,1,2,8,5,4]) >>>grp22=set([1,9,3,2,5]) >>>grp1 # printing set1 >>>grp2 #printing set2
  40. Practice >>>intersection = grp1 & grp2 #intersection >>>intersection >>>union=grp1 | grp2 # Union of grp1 & grp2 >>>union >>>difference=grp1-grp2 >>>sym_diff = grp1 ^ grp2
  41. Type() function type() function is a built-in function which returns the datatype of an arbitrary object.
  42. Practice >>>x=10 >>>type(x) >>>type(‘hello’) >>>import os >>>type(os) >>>t=(1,2,3) >>>type(t) >>>li=[1,2,3] >>>type(li)
  43. INPUT FROM KEYBOARD 1. input() 2. raw_input()
  44. Input() function • Input() function interprets the input provided by the user, i.e. if user provides an integer value as input then the input function will return its integer value. • On the other hand, if the user has input a string, the function will return a string
  45. Practice >>>name = input(“What is your name?”) >>>print (“Hello”+name) >>>age=int(input(“Enter your age?”)) >>>age >>>hobbies=input(“What are your hobbies?”) >>>hobbies
  46. Practice >>>type(name) >>>type(age) >>>type(hobbies)
  47. Raw_input() function • It takes the input from the user but it does not interpret the input and also it returns the input of the user without doing any changes, i.e. raw. • Then, we can change this raw input into any data type which is needed for our program.
  48. Practice # No casting >>>age=raw_input(“Enter your age?”) >>>type(age) #Using type casting function to convert input to integer >>>age=int(raw_input(“What is your age?”)) >>>type(age)
  49. End…