1 / 14

COMPE 111 Introduction to Computer Engineering

Learn the basics of computer engineering programming in Python, including if statements, block structure, loops, and built-in functions. Practice exercises included.

arussell
Download Presentation

COMPE 111 Introduction to Computer Engineering

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. COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University 2010-2011

  2. 'if' statement; block structure • Block structure is determined by indentation if (condition): action1 if (condition): action1 elif (condition): action2 if (condition): action1 elif (condition): action2 else: action3 if (condition): action1 elif (condition): action2 elif (condition): action3 else: action3 if (condition): action1 else: action2

  3. Exercise

  4. Loops • Loop is a statement or group of statements that execute repeatedly until a terminating condition is satisfied • Infinite loop: A loop in which the terminating condition is never satisfied

  5. while Statement • Repetition of a block of statements • Loop until test becomes false, or 'break‘ • Explanation: “While n is greater than 0, continue displaying the value of n and then reducing the value of n by 1. When you get to 0, display the word End!”

  6. while Statement (cont.) • What are the outputs of the following programs?

  7. for Loop • Repetition of a block of statements • Explanation: The loop continues until no characters are left and prints each character.

  8. Exercise • What is the output of the following program? • The output :

  9. Built-in functions 'range(..)' • It is used to terate over a sequence of numbers: • Examples: • range(10): generates a list of 10 values starting from 0 and incrementing by value 1 (Note that 10 is notincluded) • [0,1,2,3,4,5,6,7,8,9] • range(0, 10, 2): generates values between 1 and 10 with increment value (or step value) 2 • [0,2,4,6,8]

  10. Exercise

  11. Exercise

  12. Exercise 2 • Write a program which prints the odd numbers between 1 and 150 (150 is not included)

  13. Exercise 3 • Write a program which prints the sum of numbers between 1 and 50 (50 is not included)

  14. Exercise 4 • Write a program which prints the prime numbers between 2 and 100 (100 is not included)

More Related