Introduction to Python
Welcome Congratulations for deciding to participate in a 7 days of Introduction to Python Course. In this course you will learn the basics of programming using Python.
Course Introduction This course is designed for beginners and intermediate who want to learn python programming language. In this course the topics are broken down into 7 days, where each day contains several topics with easy-to-understand explanations, real-world examples, many hands on exercises and a final project.
Why Python? Python is a programming language which is very close to human language and because of that it is easy to learn and use. Python is used by various industries and companies (including Google). It has been used to develop web applications, desktop applications, system administration, and machine learning libraries.
Installing Python  To run a python script you need to install python. Let's download python from https://www.python.org/.
Installing VS Code  Visual Studio Code is a code editor (much like any other editor like Notepad) helpful in writing and editing codes.  We download it from here https://code.visualstudio.com/
Basic Data-types  Boolean A boolean data type is either a True or False value. T and F should be always uppercase.  Integers (default for numbers) z = 5  Floats x = 3.1416  Complex Numbers x = 1+2j
Basic Data-types  Strings • Can use “ ” or ‘ ’ to specify strings • a = “Nepal” • b = ‘Kantipur Engineering College’ • “abc” == ‘abc’ • Use “ ” if ’ already in string eg: “matt’s” • Use triple double-quotes for multi-line strings or strings than contain both ‘ and “ inside of them: “““a‘b“c”””
Checking Data Types  To check the data type of certain data/variable we use the type function. >>> type(15) <class ‘int’> >>> type(3.14) <class ‘float’> >>> type(1+2j) <class ‘complex’> >>> str = “Hello World” >>> type(str) <class ‘string’>
Naming Rules  Names are case sensitive and cannot start with a number.  They can contain letters, numbers, and underscores.  bob Bob _bob _2_bob_ bob_2 BoB  There are some reserved words: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while  Can’t do this • for = 12 #for is a reserved word
Assignment  Assign value to variables >>> x = 2  You can assign to multiple names at the same time >>> x, y = 2, 3 >>> x 2 >>> y 3  This makes it easy to swap values >>> x, y = y, x  Assignments can be chained >>> a = b = x = 2
Mathematical Operators
Mathematical Operators  Exercise: Find an Euclidian distance between (2, 3) and (10, 8)
Mathematical Operators  Exercise: For a given temperature in celsius stored in variable ‘t_c’, get the Fahrenheit temperature in ‘t_f’ and display result. • Let t_c = 100 • ans = 212
Checking Data Types  Exercise: What would be the data-type of? >>> a = 4/3 >>> type(a) >>> n = 4//3 >>> type(a) >>> num = ‘2080’ >>> type(num)
Type Casting  Converting one data type to another data type.  We use int(), float(), str() >> # float to int >> gravity = 9.81 >> print(int(gravity))
Type Casting  When we do arithmetic operations string numbers should be first converted to int or float otherwise it will return an error. >> #str to int or float >> num_str = '10.6‘ >> print('num_int', int(num_str)) >> print('num_float', float(num_str))
Comparison Operators
Comparison Operators  In programming we compare values, we use comparison operators to compare two values. We check if a value is greater or less or equal to other value. >> 2 == 2 >> 3.14 == 3.1416 >> print(123 == ‘123’) >> print(123 == int(‘123’)) >> a = ‘mango’ >> b = ‘orange’ >> a == b >> a < b What does this show?
Logical Operators
Logical Operators  Logical operators are used to combine conditional statements: >> 2 == 2 and 2 < 4 True >> print() >> >> a = ‘mango’ >> b = ‘orange’ >> a == b >> a < b What does this show?
Conditionals  In python the key word if is used to check if a condition is true and to execute the block code. Remember the indentation after the colon. a = 10 if a > 3: print(a, “is greater than 3”)  if else a = 3 if a > 0: print(a, “is positive number”) else: print(a, “is negative”)
Conditionals  if elif else a = 3 if a > 0: print(a, “is positive number”) elif a < 0: print(a, “is negative number”) else: print(a, “is zero”)
Operators and Conditionals  Exercise:  How do you check if a number is between 5 and 10 inclusive? Note: Use if statement here >>> if condition_1 and condition_2: ... choice_1 >>> else: ... choice_2 >>>
Operators and Conditionals  Exercise:  How do you check if a number is even or not using python? Note: Use if statement here >>> if condition: ... choice_1 >>> else: ... choice_2 >>>
Program for the Day Calculate Electricity Bill (15A) KWh Minimum Charge Charge per KWh 0 to 20 50 4.00 21 to 30 75 6.50 31 to 50 75 8.00 51 to 100 100 9.50 101 to 250 125 9.50 250 above 175 11 Ans: 2330 for 250 units
Loops  In programming we also do lots of repetitive tasks. In order to handle repetitive task programming languages use loops. Python programming language also provides the following types of two loops: • while loop • for loop
While Loop count = 0 while count < 5: print(count) count = count + 1 #prints from 0 to 4
for Loop # syntax for iterator in range(start, end, step): #loop statements for i in range(1,10,1): print(i)
for Loop num_list = [1,2,3,4,5] for num in num_list: print(num) it_companies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', ‘Amazon‘] for company in it_companies: print(company)
Program for the Day Student Grading SEE and NEB
Program for the Day Student Grading SEE and NEB  For a given students’ mark in percentage, display letter grade for each student. marks = 90 Your code should display “Outstanding”
Program for the Day Student Grading SEE and NEB  For a list of students’ marks in percentage, display letter grade for each student. marks = [95, 42, 78, 45, 89, 90] Use for loop

introduction to python in english presentation file

  • 1.
  • 2.
    Welcome Congratulations for decidingto participate in a 7 days of Introduction to Python Course. In this course you will learn the basics of programming using Python.
  • 3.
    Course Introduction This courseis designed for beginners and intermediate who want to learn python programming language. In this course the topics are broken down into 7 days, where each day contains several topics with easy-to-understand explanations, real-world examples, many hands on exercises and a final project.
  • 4.
    Why Python? Python isa programming language which is very close to human language and because of that it is easy to learn and use. Python is used by various industries and companies (including Google). It has been used to develop web applications, desktop applications, system administration, and machine learning libraries.
  • 5.
    Installing Python  Torun a python script you need to install python. Let's download python from https://www.python.org/.
  • 6.
    Installing VS Code Visual Studio Code is a code editor (much like any other editor like Notepad) helpful in writing and editing codes.  We download it from here https://code.visualstudio.com/
  • 7.
    Basic Data-types  Boolean Aboolean data type is either a True or False value. T and F should be always uppercase.  Integers (default for numbers) z = 5  Floats x = 3.1416  Complex Numbers x = 1+2j
  • 8.
    Basic Data-types  Strings •Can use “ ” or ‘ ’ to specify strings • a = “Nepal” • b = ‘Kantipur Engineering College’ • “abc” == ‘abc’ • Use “ ” if ’ already in string eg: “matt’s” • Use triple double-quotes for multi-line strings or strings than contain both ‘ and “ inside of them: “““a‘b“c”””
  • 9.
    Checking Data Types To check the data type of certain data/variable we use the type function. >>> type(15) <class ‘int’> >>> type(3.14) <class ‘float’> >>> type(1+2j) <class ‘complex’> >>> str = “Hello World” >>> type(str) <class ‘string’>
  • 10.
    Naming Rules  Namesare case sensitive and cannot start with a number.  They can contain letters, numbers, and underscores.  bob Bob _bob _2_bob_ bob_2 BoB  There are some reserved words: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while  Can’t do this • for = 12 #for is a reserved word
  • 11.
    Assignment  Assign valueto variables >>> x = 2  You can assign to multiple names at the same time >>> x, y = 2, 3 >>> x 2 >>> y 3  This makes it easy to swap values >>> x, y = y, x  Assignments can be chained >>> a = b = x = 2
  • 12.
  • 13.
    Mathematical Operators  Exercise:Find an Euclidian distance between (2, 3) and (10, 8)
  • 14.
    Mathematical Operators  Exercise:For a given temperature in celsius stored in variable ‘t_c’, get the Fahrenheit temperature in ‘t_f’ and display result. • Let t_c = 100 • ans = 212
  • 15.
    Checking Data Types Exercise: What would be the data-type of? >>> a = 4/3 >>> type(a) >>> n = 4//3 >>> type(a) >>> num = ‘2080’ >>> type(num)
  • 16.
    Type Casting  Convertingone data type to another data type.  We use int(), float(), str() >> # float to int >> gravity = 9.81 >> print(int(gravity))
  • 17.
    Type Casting  Whenwe do arithmetic operations string numbers should be first converted to int or float otherwise it will return an error. >> #str to int or float >> num_str = '10.6‘ >> print('num_int', int(num_str)) >> print('num_float', float(num_str))
  • 18.
  • 19.
    Comparison Operators  Inprogramming we compare values, we use comparison operators to compare two values. We check if a value is greater or less or equal to other value. >> 2 == 2 >> 3.14 == 3.1416 >> print(123 == ‘123’) >> print(123 == int(‘123’)) >> a = ‘mango’ >> b = ‘orange’ >> a == b >> a < b What does this show?
  • 20.
  • 21.
    Logical Operators  Logicaloperators are used to combine conditional statements: >> 2 == 2 and 2 < 4 True >> print() >> >> a = ‘mango’ >> b = ‘orange’ >> a == b >> a < b What does this show?
  • 22.
    Conditionals  In pythonthe key word if is used to check if a condition is true and to execute the block code. Remember the indentation after the colon. a = 10 if a > 3: print(a, “is greater than 3”)  if else a = 3 if a > 0: print(a, “is positive number”) else: print(a, “is negative”)
  • 23.
    Conditionals  if elifelse a = 3 if a > 0: print(a, “is positive number”) elif a < 0: print(a, “is negative number”) else: print(a, “is zero”)
  • 24.
    Operators and Conditionals Exercise:  How do you check if a number is between 5 and 10 inclusive? Note: Use if statement here >>> if condition_1 and condition_2: ... choice_1 >>> else: ... choice_2 >>>
  • 25.
    Operators and Conditionals Exercise:  How do you check if a number is even or not using python? Note: Use if statement here >>> if condition: ... choice_1 >>> else: ... choice_2 >>>
  • 26.
    Program for theDay Calculate Electricity Bill (15A) KWh Minimum Charge Charge per KWh 0 to 20 50 4.00 21 to 30 75 6.50 31 to 50 75 8.00 51 to 100 100 9.50 101 to 250 125 9.50 250 above 175 11 Ans: 2330 for 250 units
  • 27.
    Loops  In programmingwe also do lots of repetitive tasks. In order to handle repetitive task programming languages use loops. Python programming language also provides the following types of two loops: • while loop • for loop
  • 28.
    While Loop count =0 while count < 5: print(count) count = count + 1 #prints from 0 to 4
  • 29.
    for Loop # syntax foriterator in range(start, end, step): #loop statements for i in range(1,10,1): print(i)
  • 30.
    for Loop num_list =[1,2,3,4,5] for num in num_list: print(num) it_companies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', ‘Amazon‘] for company in it_companies: print(company)
  • 31.
    Program for theDay Student Grading SEE and NEB
  • 32.
    Program for theDay Student Grading SEE and NEB  For a given students’ mark in percentage, display letter grade for each student. marks = 90 Your code should display “Outstanding”
  • 33.
    Program for theDay Student Grading SEE and NEB  For a list of students’ marks in percentage, display letter grade for each student. marks = [95, 42, 78, 45, 89, 90] Use for loop