170 likes | 263 Views
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("hello, world<br>"); }. “Hello World” in JAVA. class myfirstjavaprog {
E N D
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("hello, world\n"); }
“Hello World” in JAVA class myfirstjavaprog { public static void main(String args[]) { System.out.println("Hello World!"); } }
“Hello World” in Python print (“Hello World!!”)
Python – Interpreter Shell >>> print ("Hello, World! ") Hello, World! >>> "hello" 'hello' >>> "world" 'world' >>> "hello"+"world“ (Concatenation) 'helloworld'
Be familiar with Python >>> "hello" * 3 (Multi string repeat) 'hellohellohello' >>> "hello" * 300
How do you run programs? Three different methods: • 1. Interactive Coding • 2. Files (such as NotePad, WordPad) • 3. Integrated Development Environment (IDE)
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)
Formatting and Line • \n – moves string to the next line (like hitting the ‘return’ button) • \t - tabs the following string
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.
Examples: Formatting Strings and Lines print(“Money can’t buy me happiness,\nBut I’d rather be crying\tin my mansion.”)
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.
But Wait! There is more! • What if you want to print the following? I’m just too “cool” for the program!
Error! Bad! print(“I’m just too “cool” for the program!”) Good! print(“I’m just too \“cool\” for the program!”)
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”
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)