210 likes | 224 Views
Learn the fundamentals of Python coding and problem-solving strategies. Understand key terminology, functions, and syntax for efficient programming. Explore Python’s power and versatility with real-world applications.
E N D
Problem Solving Warm-Up Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): 8 8 8 = 6
How to Problem Solve • https://www.youtube.com/watch?v=zrzMhU_4m-g • The single most important skill for a computer scientist!!!!!!
Think like a computer programmer! • This way of thinking combines some of the best features of mathematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.
Think Python! • The programming language you will learn is Python. • Python is an example of a high-level language; other high-level languages you might have heard of are C, C++, Perl, and Java.
PythonChapter 1 – Getting Started What is Python and why do we use it? • Python is an easy to use programming language: you can use it to create web apps, games, and even a search engine. • Developed by Guido van Rossum and released in 1991 • Python got it’s name from the movie Monty Python • Python is easy to use • Bridges the gap between programmers brain and the computer • Clear and simple rules make the language very close to English • Straightforward language – it has been called “programming at the speed of thought” • Programs are shorter and take less time to create
Python is powerful • Used by companies such as Google, IBM, Microsoft, Verizon, Electronic Arts (EA Sports), Disney Interactive • Python is a “glue” language • Python runs everywhere • Can be integrated into other languages Java, C++, etc. • Python is free! • Can install on any computer and not pay a penny!
Some Important Terminology Functions – like a mini program that goes off and performs a task. • You kick off or call a function by using the function name, followed by a set of parenthesis. • You can give or pass a function values to work with. • These values, called arguments are put inside the parenthesis. Ex. Print (“Game Over”) displays Game Over argument function *Note: Python functions are case sensitive – Print works, PRINT won’t
String – series of characters • A string can contain letters, numbers, and symbols. Ex: hello, 123abc, i love school Statement – a complete instruction Ex: print(“Game Over”) • In English, a statement is a complete thought, in Python it is a complete instruction. It does something. Programs are made up of a number of statements Code – programming statements
Color Coding • Python uses color coding, called syntax highlighting , to help you understand what you have typed. • Function Purple • Strings Green • Output Blue
Introducing IDLE Python comes with an integrated development environment called IDLE. A development environment is a set of tools that makes writing programs easier. You can think of it as a word processor for your programs. IDLE provides two modes in which to work: an interactive mode and a script mode. Programming in Interactive Mode Time to write your first program! In this mode, you can tell Python what to do and it will respond immediately. Start up your Python Programs!
To begin: Start Menu > All Programs > Python 3.1 > IDLE (Python GUI) You should see a screen similar to this: This window is called the python shell These three arrows are a command prompt – ready for you to give instructions
At the command prompt type print(“Game Over”) and then press the Enter Key
Function • The print() function can display text, surrounded by quotes, that you put inside the parenthesis. If you put nothing it will print a blank line. String called string literal, because it is literally the sequence of characters that make up that word Output
Generating an Error • Computers take everything literally, if you misspell a function it will have no idea what you mean. Try typing: primt(“Game Over”)
Programming in Script Mode • Interactive mode gives you immediate feedback, but you can’t save programs and run them later. • In script mode you can write, edit, load, and save your programs. • Like a word processor for your code, you can even find and replace, and cut and paste.
Write your first program (again) • Type print(“Game Over”) and press Enter • Nothing happens! This is because you are writing a list of statements to be executed later. • Once you save your program, you can run it.
To save your program, select File > Save as.*For now just save it to your desktop • Give the program the name game_over.py *Make sure you save all programs with the .py extension. This allows various applications including IDLE to recognize these files as Python programs. • To run, select Run > Run Module • Notice your interactive window still contains old text from before • Try running the program from your desktop – double click it. • What happens?
We need to add a line to the end of all of our programs so that the program won’t close until the user hits enter. This line is input(“\n\nPress the enter key to exit.”) Now once the user presses the Enter key, the program exits and the console window will disappear. *Note: The computer ignores blank lines. These are just for humans reading code. Blank lines make programs easier to read, so use them when you write. Keep related code together and separate sections with a blank line
And finally, Using Comments • Can be written at any part in a program • Color code red • Computer ignores them – they are meant for humans • Help you remember the goal of your program and VERY useful to other programmers. • Create using a number sign symbol. • Ex: # Game Over # Demonstrates the print functions
Anything after this symbol (except in a string) on the rest of the line is a comment. • You should start all of your programs with a few comments – it is helpful to list the title and purpose of the program. You should also list the name of the programmer and the date the program was written. • Go back and add these comments to the beginning of your program End of Chapter 1!