190 likes | 193 Views
Introduction to programming with Python. Faiza Qadri Graduate Student,Department of Engineering,SSU Society of Women Engineering Session:01. Languages. Why is it important.
E N D
Introduction to programming with • Python Faiza Qadri Graduate Student,Department of Engineering,SSU Society of Women Engineering Session:01
Why is it important • Python started as a scripting language for Linux like Perl but less cryptic. Now it is used for both web and desktop applications and is available on Windows too. • According to IEEE Spectrum,its one of the top 5 software languages • Must have skill for a Software Engineer! • One of the official languages at Google • NASA uses Python! (https://www.python.org/about/success/usa/) • Courses it is useful in:Wireless Systems,Networking,Analog & Digital Systems
A little History • Python programming language was created by Guido Van Rossum in 1991 at at the National Research Institute for Mathematics and Computer Science in the Netherlands. • The inventor is a fan of "Monty Python's Flying Circus", a comedy series from the seventies. • Python is derived from many other languages, including C, C++, SmallTalk, and Unix shell and other scripting languages. • Its design philosophy emphasizes code readability
Key Features • interpret • compile • execute • source code • Hello.java • byte code • Hello.class • output • source code • Hello.py • output • Python is a high-level,interpreted,interactive and object-oriented scripting language • Python is Interpreted:Similar to PERL and PHP • Many languages require you to compile (translate) your program into a form that the machine understands. • Python is instead directly interpreted into machine instructions.
Key Features • Python is Interactive • Python is Object-oriented • Python is a Beginner's Language
Lets get started!! • Python Official Website : http://www.python.org/
Program #1 • (Input ,Print) • Input: Reads a number from user input. • You can assign (store) the result of input into a variable. • Example: age = input("How old are you?”) n=eval(input(‘enter the input’)) print (“Your age is", age) print (“You have", 65 - age, "years until retirement”) • print : Produces text output on the console. • Syntax: print (“Message”) print (Expression) • Program #1 Number Game:Program that takes any 2-digit number and outputs a 6-digit number,having the 2-digit repeated.For-example,24 becomes 242424.
Math commands • Python has useful commands for performing calculations. • To use many of these commands, you must write the following at top of your Python program: • import math
Program #02 Program #2 • Saving Interest Program • Concepts:String Formatting,Functions,Modules
Branching Branching • A program should be able to execute different statements depending on calculations or users input. • If-else if expression: statements else: Statements if expression is false Statements here execute after the if-else. • Multiple if-else,Use elif i if condition: statements elif condition: statements else: statements • Program #3 Tweet Decoder:Write a program that decodes a few common abbreviations (tweets) or email and provides the corresponding English Phrase.
Lists & Dicts • A list is a container, which is an object that groups related objects together. • A list is also a sequence; thus, the contained objects maintain a left-to-right positional ordering.For example list1 = ['physics', 'chemistry', 1997, 2000]; list 2=[1,2,3,4,5]; list3 = [“a”,“b”,“c”,“d”] • List Methods:list.append(x) , list.insert(i,x) , list.remove(x), list.pop(0) • A dictionary is another sort of type of container • Dictionaries contain references to objects as key-value pairs – each key in the dictionary is associated with a value .For-example dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': ‘3258'} dict1 = { 'abc': 456 }; dict2 = { 'abc': 123, 98.6: 37 };
Program • Extract IP address program
Loops Programmer can use a loop that executes the same code over and over again as long as some condition is true. While Loop while expression: # Loop expression # Loop body: Sub-statements to execute... # if the loop expression evaluates to True... # Statements to execute after the expression evaluates to False
Loops For Loop Syntax for variable in container: # Loop body: Sub-statements to execute # for each item in the container # Statements to execute after the for loop is complete Example:Square of a Number: for x in range(1, 6): print (“squared is", x * x) Program #4 Write a program to find the Factorial of a number(Use While or For Loop)’ Notes:2 things about loops:No of times we want to run the loop,There has to be an exit condition
Nested Loops • Loop inside a loop • Program#05 Write a program that uses a nested for-loop to find the prime numbers from 2 to 100
Program #06 • Phone Number Program • Program allows the user to enter a phone number that includes letters, which appear on phone keypads along with numbers, as is commonly used by companies as a marketing tactic (e.g., 1-555-HOLIDAY). The program then outputs the phone number using numbers only.
Continued.. • Session :02 • OOP