📚 Python Basics in IDLE
Brief Overview
This note covers Python programming basics and was created from a 55‑minute audio lecture. It
introduces IDLE setup, explains variables and conditionals, demonstrates user input handling, covers
function creation, and walks through a palindrome detection example.
Key Points
Setting up IDLE and working with files
Variables, operators, and conditional statements
Getting and converting user input
Defining and calling functions for unit conversion and palindrome checks
🖥️ Setting Up IDLE and Files
Open IDLE by typing “IDLE” in the search box and selecting the application.
Create a new file:
1. Click File → New File.
2. Save the file (e.g., test.py) in any folder you have write permission for
(Documents, Desktop, Downloads, etc.).
The name of the file appears at the top of the editor window.
Definition – IDLE is the integrated development environment that ships with Python, providing an
editor and an interactive shell.
📄 Working with Line Numbers and Basic Syntax
Enable line numbers via Options → Configure IDLE → Show line numbers.
Each line receives a sequential number (e.g., line 1, line 2).
Important distinction:
Assignment operator = stores a value in a variable.
Equality operator == compares two values.
Operator
Purpose
Example
=
Assignment
x=1
==
Comparison
x == 1
🔢 Variables and Assignment
Declare a variable and assign a value:
x = 1
Changing the value simply reassigns:
x = 3
Definition – A variable is a named storage location that holds a value which can be changed during
program execution.
✅ Conditional Statements
If statement checks a condition using == and executes the indented block when true.
if x == 1:
print("yes")
Else clause provides an alternative block when the if condition is false.
if x == 1:
print("yes")
else:
print("x is not equal to 1")
Multiple independent if statements evaluate each condition separately, while else attaches
to the preceding if.
Definition – A conditional statement directs program flow based on whether a Boolean expression
evaluates to True or False.
📥 Getting User Input
Use input() to prompt the user; it returns a string.
Convert the string to an integer with int().
x = int(input("Enter a number: "))
After conversion, x can be compared with numeric literals using ==.
Definition – The input() function reads a line of text from the console; int() casts that text to an
integer type.
🧩 Functions Overview
A function groups reusable code under a name, optionally accepting parameters (input
values).
Metaphor: a fry function receives an item, places it in a fryer, and returns the cooked result.
def fry(item):
# imagine putting the item in a fryer
cooked = item # placeholder for actual cooking process
return cooked
Calling a function executes its body with specific arguments:
fried_fries = fry("French fries")
Definition – A function is a block of reusable code that can accept arguments, perform operations,
and optionally return a value.
📏 Example: Converting Yards to Feet
Conversion formula:
feet = yards × 3
Implemented as a function:
def yards_to_feet(yards):
return yards * 3
Use the function with user input:
y = int(input("Enter yards: "))
print(yards_to_feet(y), "feet")
📏 Unit Conversion in Python
🔢 Multiplication for Simple Conversions
To convert yards to feet, multiply the yard value by 3.
Example shown: printing the number of yards multiplied by 2 (demonstrates basic
arithmetic).
Definition: Multiplication is the arithmetic operation used to scale a value by a constant factor.
📐 Conversion Factors Mentioned
Unit
Conversion Factor
yards → feet
1 yard = 3 feet
yards → inches
1 yard = 86 inches
🖥️ Using input() for User Interaction
(as stated in the transcript)
input() captures user‑provided text; the result is a string.
To use the value in calculations, convert the string to a numeric type (e.g., int() or float()).
The program asks two inputs:
1. Number of yards (x).
2. Desired conversion unit (q), entered as a single character (F for feet, I for inches).
Definition: input() is a built‑in Python function that reads a line of text from the user.
🧩 Conditional Logic for Selecting Conversion
Case‑sensitive check is used:
Condition
q == 'f' (lowercase)
Action
Call the feet conversion function.
q != 'f' (any other character)
Call the inches conversion function.
The else block handles all inputs that are not a lowercase f.
Assumes the user will enter either f (or F) for feet or i (or I) for inches.
Definition: A conditional statement (if … else) executes different code blocks based on whether a
Boolean expression evaluates to True or False.
🛠️ Defining Conversion Functions
Two separate functions are created: one for feet and one for inches.
Each function receives the yard value x and prints the converted result.
Definition: A function is a reusable block of code that performs a specific task and can accept
input arguments.
🗂️ Working with Files and IDLE
📂 Opening a Python Challenge File
Download the challenge solution (e.g., Homework3_iPhone_R25.py).
Right‑click the file → Open With → select IDLE (Python’s integrated development
environment).
If the file is associated with another program (e.g., Microsoft Word), manually launch IDLE
first, then open the file from IDLE’s File → Open menu.
⚙️ File Association Issue
The OS determines which application opens a file based on its association.
Changing the default program or using Open With resolves mismatched associations.
📚 Resources and Learning Path
Chapter
Topics Covered
Getting Started
How to launch IDLE, file navigation.
Basics of Python
print, input, data types (int, float, str).
Conditionals
if, if … else structures.
Loops
while loop syntax and behavior.
Functions
Defining and calling functions (e.g., feet and
inches converters).
The LinkedIn Learning module provides video lessons and practice exercises for each
chapter.
Students are encouraged to review these chapters to reinforce concepts demonstrated in
class.
📝 Code Documentation and Comments
Comments start with # and appear in red in IDLE.
The interpreter ignores comments; they serve only for human readers.
Requirement: Every code block must be preceded by a comment explaining its purpose.
Definition: A comment is non‑executable text in source code that documents intent, logic, or
usage.
🔤 Palindrome Concept
Definition: A palindrome is a word, phrase, or sequence that reads the same forward and
backward (e.g., “radar”, “level”).
Example discussed: “Randall” is not a palindrome because its reverse (“lladnaR”) differs
from the original.
🔁 Palindrome Detection Overview
Palindrome – a word or phrase that reads the same backward as forward (e.g., Bob, racecar).
🖥️ Program Flow
Input Loop
Variable run is set to True.
while run: executes repeatedly until run becomes False.
Exit Condition
If the user enters exit, testString == "exit" triggers run = False, ending the loop.
User Prompt
The program asks for a string to test for palindrome status.
🧩 Key Functions & Operations
Function / Operation
Purpose
Note
isPalindrome(s)
Returns True if s equals its
reverse, otherwise False.
Uses slicing to reverse the
string.
s[::-1] (slice)
Produces the reversed version
of s.
Described as “the slice function
… it reverses the string.”
s.lower()
Converts all characters in s to
lowercase.
Ensures case‑insensitivity (e.g.,
F vs f).
Loop over characters
Builds newString by excluding
numbers, spaces, and
punctuation.
Only letters remain for
palindrome testing.
Slice Operation – In Python, string[::-1] creates a reversed copy of string.
📊 Step‑by‑Step Algorithm
1. Read Input – Assign user entry to testString.
2. Check for Exit – If testString == "exit", set run = False and break loop.
3. Normalize Case – testString = testString.lower().
4. Remove Non‑letters
Initialize newString = "".
For each character c in testString:
If c is a letter, append to newString.
5. Palindrome Test – Call isPalindrome(newString).
If True, output “*, is a palindrome.”
If False, output “*, is not a palindrome.”
6. Prompt to Run Again – Loop returns to step 1 while run remains True.
🛠️ Sample Python Implementation
def isPalindrome(s):
"""Return True if s reads the same backward as forward."""
return s == s[::-1]
run = True
while run:
testString = input("Enter a string to test for palindrome (or type 'exit'): ")
if testString == "exit":
run = False
continue
# Normalize and clean input
testString = testString.lower()
newString = ""
for ch in testString:
if ch.isalpha():
newString += ch
# Check palindrome status
if isPalindrome(newString):
print(f"'{testString}' is a palindrome.")
else:
print(f"'{testString}' is NOT a palindrome.")