This document serves as an introduction to Python programming for non-majors, covering its history, advantages, and coding environments. It explains the basics of the Python interpreter, comments, and variables, along with practical examples and exercises. The course will utilize Python 3.9 and emphasizes the importance of good programming practices.
This document serves as an introduction to Python programming for non-majors, covering its history, advantages, and coding environments. It explains the basics of the Python interpreter, comments, and variables, along with practical examples and exercises. The course will utilize Python 3.9 and emphasizes the importance of good programming practices.
Outline +Why Python +Coding environments +Python interpreter +Comments +Variables What is Python? +Python is a general-purpose interpreted programming language. +Python was created in 1991 by Guido van Rossum. The name "Python" was inspired by Monty Python's Flying Circus, a popular 1970s British comedy group. +Here’s one of Monty Python's Flying Circus skits on YouTube: https://www.youtube.com/watch?v=0D7hFHfLEyk Why Python? +Python is a great language for beginners and experienced programmers alike, and it is widely used in a variety of fields. +There are many reasons why Python is a popular programming language: o Easy to learn and use o Versatile o Large and active community of users and developers o Good support for third-party libraries and frameworks Python environs Python environs cont… +IDLE: IDLE (Integrated Development and Learning Environment) is the default Python editor that comes with the standard Python distribution. It is a GUI-based software tool that provides a user- friendly interface for writing, testing, and debugging Python code. IDLE includes features such as syntax highlighting, line numbering, and automatic indentation, making it a convenient choice for new and experienced Python programmers alike. +Other Python integrated development environments (IDEs), text editors and packages include: Anaconda, Jupyter Notebook, PyCharm, Spyder, VS Code, Sublime Text Editor, etc Python environs cont… IDLE interactive console syntax highlighting: Python interpreter +Interpreted: o Not compiled like Java o Code is written and then directly executed by an interpreter o Type commands into interpreter and see immediate results
Java: Runtime Code Compiler Computer Environment
Python: Code Interpreter Computer
Python interpreter cont… • The interpreter allows you to type commands one-at-a-time and see results • It is a great way to explore Python's syntax Hello World… +Let’s write our first Python Program - Hello World +Here are the steps: 1. Open IDLE 2. Enter the text after >>> 3. print(“Hello World!”) 4. Hit “ENTER” key Interactive console as a calculator + You can use the Python interactive console as a calculator. + First, navigate to Python IDLE on your computer and you can type in mathematical expressions and the console will print out the result. + Here’s an example: >>> 2 + 2 4 >>> 5 * 8 40 >>> (3 + 4) * 5 35 >>> 2 ** 3 8 Comments + A comment is a piece of text in the source code of a program that is ignored by the interpreter. Comments are used to provide human-readable explanations of the code and to make the code easier to understand. + Comments are indicated using the # character. Anything following the # on a line is considered a comment and will be ignored by the interpreter: # This is a comment x = 10 # This is also a comment + Comments can also span multiple lines by enclosing them in triple quotes: """ This is a multi-line comment. It can span multiple lines. """ Variables + A variable is a named identifier that refers to a value. Variables are used to store values in programs so that they can be used later on. + To create a variable in Python, you simply need to assign a value to a name. x = 100 + Use any combination of letters, digits, and underscores to name variables, as long as the name does not begin with a digit. + Its good programming practice to choose descriptive names for variables, so that its clear what purpose each variable serve in your program. + Here are examples of valid variable names: age year2023 reg_number Python versions + Syntax changes: In order to make it easier to read and write. For example, in Python 3.0, the print statement was changed to a print() function. + Standard library changes: The Python standard library is a collection of modules that are included with every Python installation. These modules provide a wide range of functions and features, and they are frequently updated and improved. + Performance improvements: Each new version of Python is typically faster and more efficient than the previous version, i.e., addition of new features. + Deprecation of old features: With new versions, some features are deprecated. This means that they may still be available in newer versions of Python, but they are no longer recommended for use, and they may be removed in a future version. + New features: Each new version of Python typically introduces a number of new features and improvements. + In summary, we will be using Python 3.9 in this course. Practice Work 1. Identify valid variable names from the following list: a) Full name b) _ c) @user d) 12students e) CLASS f) IntroductionToComputerProgrammingUsingPython g) Year1 h) CodingZ2easy 2. Write a program that prints “Hello Pythoneer!” to the console. The art and science of asking questions is the source of all knowledge.