Introduction to Python
Why is Python? Object-oriented Free (open source) Portable Powerful Mixable Easy to use Easy to learn
Running Python Immediate mode Script mode Integrated Development Environment (IDE) >>> print( hello world ) hello world
Keywords Variable name X Function name X Case sensitive
Keywords(Contd.)
Identifiers Combination of Letters (lowercase or uppercase) Digits (o to 9) Underscore (_) Example : myclass, Var_1 etc. Cannot start with digit. Example : 1var (wrong), var1 (right) Any length Keywords cannot be used Cannot use special symbols like :, @, $, % etc.
Remember Case sensitive Example : var and Var are different. Appropriate naming Example : count = 10 instead of c = 10. Multiple words can be separated using underscore Example : This_is_a_long_variable Camel-case style Example : camelcaseexample
Lines and Indentation No braces to indicate blocks Indentation must be of same amount Example : if True : print ( True ) else : print ( False )
Line continuation character (\) Example : Total = 1 + 2 + 3 \ 4 + 5 + 6 Multi-Line Statements [], {}, () don t need (\) Example : days = [ Monday, Tuesday, Wednesday, Thursday, Friday ]
Quotation ( ), ( ), ( ) or ( ) can be used. Example 1 : word = word Example 2 : paragaph = This is a paragraph
Comments One line Example : # This is a comment print( First comment ) Multiple line Example : # This is a # multiple line comment print( Multiple line comment )
Declaring Variables Assigning value to a variable Example : name = python print (name) Changing value of a variable Example : x = 2 x = 3 print (x)
Declaring Variables (Contd.) Assigning multiple values to multiple variables Example 1 : a, b, c = 5, 3.2, hello print ( a=, a, b =, b, c =, c) Example 2 : x = y = z = 1 print( x =, x, y =, y, z =, z)
Rules and naming convension Create an appropriate name camelcase notation Never use special symbols!, @, #, $, % etc Don t start with digit Combination of letters or digits or underscore
Literals Numeric Literals String Literals Boolean Literals Special Literals Literal Collection
Literals (Contd.) Numeric Literals Example : integer1 = 10 float1 = 19.7 String Literals Example : string1 = We are learning python
Literals(Contd.) Boolean Literals Example : x = (1 == True) y = (1 == False) Special Literals Example : food = None Literal Collections Example : fruits = [ apple, orange, banana ] integer_list = [1, 2, 3] vowels = { a, e, i, o, u }
Data Types Numbers Strings List Tuple Set Dictionary
Data Types (Contd.) Numbers Example : var1 = 7 var2 = 6.8 Strings Example : str = hello world List Example : lst = [1, 6, 9, 3, 7] print(lst) print(lst[0:2])
Data Types (Contd.) Tuples Example : tupl = ( a, b, c, d, e ) print(tupl[1:4]) Set Example : a = {5, 9, 7, 2, 8}
Data Types (Contd.) Dictionary Example : tinydict = {1: Monday, 2: Tuesday, 3: Wednesday } print(tinydict.keys()) print(tinydict.values()) print(tinydict[1])
Question numbers = [3, 6, 9, 12, 15, 18]. What is the type of numbers? Tuple List Set Dictionary
Question numbers = [3, 6, 9, 12, 15, 18]. What is the type of the elements in numbers? String Integer Float
Question numbers = [3, 6, 9, 12, 15, 18]. What is the result for numbers[2:4]? [6, 9, 12] [9, 12] [9, 12, 15] [6, 9]
Question student_record = { name : john, id : 806, dept = statistics }. What is the type of student_record? List Dictionary Set
Question student_record = { name : john, id : 806, dept = statistics }. What is the result for student_record[ dept ]? john 806 statistics
Question Valid name for a variable - 4digits _digits digit_s digit$
color = red color = green color = blue What will be the result for print(color)? red green blue Question