Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Links | Search | Bio |

Guide to Python (University at Buffalo Version)


Chapter 1: Introduction to Python

Python is a popular general-purpose scripting language that is especially suited computer program development. (See python.org). Python is available for for Windows, Linux/UNIX, macOS, and many other operating systems/platforms from the Python Download site.

Hello World!

Most beginning programmers start with the standard "Hello World" code:

A Python Hello World Code Example

Python 3.12.8 (tags/v3.12.8:2dc476b, Dec  3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print(Hello!)
  
Figure 1-1: Python Console Code for helloworld.py

The output would look like this:

Hello!
>>>
  
Figure 1-2: Hello World Python Output
IDLE Console
Figure 1-2a: IDLE Console Window

Python Comments

Python provides a few different way to add comments to your code. You can use multi-line comments or inline (single line) comments or Python Docstrings comments. Comments should be used to explain what your code is doing. Comments should be used to provide a description of each function including whether there are parameters and whther the function returns a value or not.

"""
Assignment: My First Python program
Author: Your Name Here
Date: Month Day, Year file was last edited
Editor: Notepad++ or TextWrangler or idle or whatever editor you used
"""
# Print hello string
def printName():
    """ 
    prompt for a name
    then print the hello string
    """
    name = input("Enter your name: ")
    print('Hello ' + name)

# Call the printName function.
printName()	


""" Python Docstring Commments """ Print hello string def printName(): """ prompt for a name then print the hello string """ Prompt for a name. name = input("Enter your name: ") print('Hello ' + name) print(printName.__doc__) help(printName) # Call the printName function. printName()
Figure 1-3: Using the input Function Code

Getting User Input

A common task is to prompt the user to input some data. You can use the Python input() function to prompt the user tp enter some data and store their response in a variable.

# get_input.py
  
# Prompt for a name.
name = input("Enter your first name: ")  
# Display a "hello" message.
print('Hello ' + name + '!')
  
Figure 1-4: Using the input Function Code
>>> Enter your name: Jim
Hello Jim!
  
Figure 1-5: Using the input Function Output

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2025 Jim Gerland