1 / 12

Introduction to Computing Using Python

Introduction to Computing Using Python. Imperative Programming. w hat is a Python program? print() statement input() statement type conversion statements str (), int (), float() if/ elif /else conditional statement. Python program. Introduction to Computing Using Python.

gema
Download Presentation

Introduction to Computing Using Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computing Using Python Imperative Programming • what is a Python program? • print() statement • input() statement • type conversion statements str(), int(), float() • if/elif/elseconditional statement

  2. Python program Introduction to Computing Using Python line1 = 'Hello Python developer...' • A Python program is a sequence of Python statements • Stored in a text file called a Python module • Executed using an IDE or “from the command line” • A Python program is a sequence of Python statements • Stored in a text file called a Python module • Executed using an IDE or “from the command line” line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) line1 = 'Hello Python developer...' line2 = 'Welcome to the world of Python!' print(line1) print(line2) $ python hello.py Hello Python developer… Welcome to the world of Python! $ python hello.py Hello Python developer… $ python hello.py hello.py

  3. Built-in function print() Introduction to Computing Using Python • Function print() prints its input argument to the IDLE window • The argument can be any object: an integer, a float, a string, a list, … • Strings are printed without quotes and “to be read by people”, rather than “to be interpreted by Python”, • The “string representation” of the object is printed >>> print(0) 0 >>> print(0.0) 0.0 >>> print('zero') zero >>> print([0, 1, 'two']) [0, 1, 'two']

  4. Built-in function input() Introduction to Computing Using Python • Function input() requests and reads input from the user interactively • It’s (optional) input argument is the request message • Typically used on the right side of an assignment statement • When executed: • The input request message is printed • When executed: • When executed: • The input request message is printed • The user enters the input • The string typed by the user is assigned to the variable on the left side of the assignment statement • When executed: • The input request message is printed • The user enters the input >>> name = input('Enter your name: ') Enter your name: Michael >>> name 'Michael' >>> ========= RESTART ============= >>> Enter your first name: Michael Enter your last name: Jordan Hello Michael Jordan... Welcome to the world of Python! >>> name = input('Enter your name: ') >>> name = input('Enter your name: ') Enter your name: Michael >>> name 'Michael' >>> ========= RESTART ============= >>> Enter your first name: Michael Enter your last name: >>> name = input('Enter your name: ') Enter your name: Michael >>> name = input('Enter your name: ') Enter your name: Michael >>> name 'Michael' >>> name = input('Enter your name: ') Enter your name: >>> name = input('Enter your name: ') Enter your name: Michael >>> name 'Michael' >>> ========= RESTART ============= >>> Enter your first name: >>> name = input('Enter your name: ') Enter your name: Michael >>> first = input('Enter your first name: ') last = input('Enter your last name: ') line1 = 'Hello’ + first + '' + last + '…' print(line1) print('Welcome to the world of Python!') first = input('Enter your first name: ') last = input('Enter your last name: ') line1 = 'Hello’ + first + '' + last + '…' print(line1) print('Welcome to the world of Python!') first = input('Enter your first name: ') last = input('Enter your last name: ') line1 = 'Hello’ + first + '' + last + '…' print(line1) print('Welcome to the world of Python!') first = input('Enter your first name: ') last = input('Enter your last name: ') line1 = 'Hello’ + first + '' + last + '…' print(line1) print('Welcome to the world of Python!') input.py

  5. Change string input to another type Introduction to Computing Using Python Function input() treats anything the user enters as a string >>> age = input('Enter your age: ') Enter your age: 18 >>> age '18' >>> intAge = int(age) >>> age 18 • What if we want the user to interactively enter non-string input such as a number? • Use a type conversion • int() changes a string, Boolean or float type to an int • float() changes a string, Boolean or int type to a float • str() changes an int, float or Boolean type to a string >>> age = input('Enter your age: ') Enter your age: 18 >>> age '18' >>> int(age) 18 >>> eval('18') 18 >>> eval('age') '18' >>> eval('[2,3+5]') [2, 8] >>> eval('x') Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> eval('x') File "<string>", line 1, in <module> NameError: name 'x' is not defined >>> >>> age = input('Enter your age: ') Enter your age: 18 >>> age '18' >>> int(age) 18

  6. Exercise Introduction to Computing Using Python Write a program that: Requests the user’s name Requests the user’s age Computes the user’s age one year from now and prints the message shown >>> Enter your name: Marie Enter your age: 17 Marie, you will be 18 next year! name = input('Enter your name: ') age = int(input('Enter your age: ')) line = name + ', you will be ' + str(age+1) + ' next year!' print(line)

  7. Exercise Introduction to Computing Using Python Write a program that: Requests the user’s name Requests the user’s age Prints a message saying whether the user is eligible to vote or not Need a way to execute a Python statement if a condition is true

  8. if statement Introduction to Computing Using Python if <condition>: <indented code block> <non-indented statement> if temp > 86: print('It is hot!') print('Be sure to drink liquids.') print('Goodbye.') if temp > 86: print('It is hot!') print('Be sure to drink liquids.') print('Goodbye.') if temp > 86: print('It is hot!') print('Be sure to drink liquids.') print('Goodbye.') The value of temp is 50. The value of temp is 90. temp > 86: True print('It is hot!') False print('Be sure to drink liquids.') Print('Goodbye.')

  9. Exercises Introduction to Computing Using Python Write corresponding if statements: If age is greater than 62 then print 'You can get Social Security benefits’ If string 'large bonuses' appears in string report then print 'Vacation time!’ If hits is greater than 10 and shield is 0 then print "You're dead..." >>> age = 45 >>> if age > 62: print('You can get Social Security benefits') >>> age = 65 >>> if age > 62: print('You can get Social Security benefits') You can get Social Security benefits >>> >>> report = 'no bonuses this year' >>> if 'large bonuses' in report: print('Vacation time!') >>> report = 'large bonuses this year' >>> if 'large bonuses' in report: print('Vacation time!') Vacation time! >>> hits = 12 >>> shield = 0 >>> if hits > 10 and shield == 0: print("You're dead...") You're dead... >>> hits, shield = 12, 2 >>> if hits > 10 and shield == 0: print("You're dead...") >>>

  10. Indentation is critical Introduction to Computing Using Python if temp > 86: print('It is hot!') print('Drink liquids.') print('Goodbye.') if temp > 86: print('It is hot!') print('Drinkliquids.') print('Goodbye.') temp > 86: temp > 86: True True print('It is hot!') print('It is hot!') False False print('Drink liquids.') print('Drink liquids.') print('Goodbye.') print('Goodbye.')

  11. if/elif/else statement Introduction to Computing Using Python if <condition>: <indented code block 1> elif: <indented code block 2> else: <indented code block 3> <non-indented statement> if age > 70: print('Elvis!') elif age > 60: print('Beatles!') else: print('Disco!') print('Goodbye') The value of age is 20. age > 70: age > 60: False False print('Disco!') True True print('Beatles!') print('Elvis!') print('Goodbye')

  12. Exercise Introduction to Computing Using Python Extend this music preference program by adding more elif clauses so that: It requests the user’s name It requests the user’s age It prints a message with the user’s musical preference for all ages, by decade >>> Enter your name: Marie Enter your age: 82 Marie, you like Elvis. >>> ============RESTART================ >>> Enter your name: Marie Enter your age: 40 Marie, you like Sinead. >>> name = input('Enter your name: ') age = eval(input('Enter your age: ')) if age > 70: print(name + ", you like Elvis.") else: print(name + ", you like Sinead.")

More Related