1 / 17

Lesson 2:Writing a Python Program

Lesson 2:Writing a Python Program. Computer Science 1 Mr. Bernstein. Hello World Program. Implement by three different languages In C In JAVA In Python. “Hello World” in C. main() { printf(&quot;hello, world<br>&quot;); }. “Hello World” in JAVA. class myfirstjavaprog {

feleti
Download Presentation

Lesson 2:Writing a Python Program

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. Lesson 2:Writing a Python Program Computer Science 1 Mr. Bernstein

  2. Hello World Program • Implement by three different languages • In C • In JAVA • In Python

  3. “Hello World” in C main() { printf("hello, world\n"); }

  4. “Hello World” in JAVA class myfirstjavaprog { public static void main(String args[]) { System.out.println("Hello World!"); } }

  5. “Hello World” in Python print (“Hello World!!”)

  6. Python – Interpreter Shell >>> print ("Hello, World! ") Hello, World! >>> "hello" 'hello' >>> "world" 'world' >>> "hello"+"world“ (Concatenation) 'helloworld'

  7. Be familiar with Python >>> "hello" * 3 (Multi string repeat) 'hellohellohello' >>> "hello" * 300

  8. How do you run programs? Three different methods: • 1. Interactive Coding • 2. Files (such as NotePad, WordPad) • 3. Integrated Development Environment (IDE)

  9. Create and run a program in the Python IDLE From File -> New Window Type your program print ("Hello, World! ") Save your program (Give a name you like, such as test.py) Run the program (by selecting the Run Module or pressing the F5 key)

  10. Formatting and Line • \n – moves string to the next line (like hitting the ‘return’ button) • \t - tabs the following string

  11. Examples: Formatting Strings and Lines • Use the print function to write the following in one line: • Money can’t buy me happiness, <return> But I’d rather be crying <tab> in my mansion.

  12. Examples: Formatting Strings and Lines print(“Money can’t buy me happiness,\nBut I’d rather be crying\tin my mansion.”)

  13. End all, BE ALL! Input: print (“Hear no evil,”, end=‘’) print (“ See no evil, Speak no evil.”) Output: Hear no evil, See no evil, Speak no evil.

  14. But Wait! There is more! • What if you want to print the following? I’m just too “cool” for the program!

  15. Error! Bad! print(“I’m just too “cool” for the program!”) Good! print(“I’m just too \“cool\” for the program!”)

  16. Formatting Strings and Lines • \” or \\ allows the print statement to continue, without closing off what is in quotes. Input: print(“Call me, \”maybe\””) Output: Call me, “maybe”

  17. Summary • Simple “Hello World” Program • Creating a Python file/Using Interpreter • Formatting Strings: \t, \n, \(character) • end’’ to keep the same line • Print function: print(“thingtoprint”) • String Operators: * (string multiplier) +(string concatenation)

More Related