Introduction to Python
Programming: Input, Output,
Operators, and Strings
What is Python?
• High-level programming language created by Guido van Rossum
• Known for its simplicity and readability
• Widely used in web development, data science, and artificial intelligence
• Named after the comedy group Monty Python
Why Learn Python?
• Easy to learn and understand
• Large community support
• Extensive library collection
• Versatile applications
• High demand in job market
• Cross-platform compatibility
Python Input Function
• Input() function reads data from keyboard
• Syntax: variable = input("prompt message")
Always returns data as string type
Example:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
Python Output Functions
• print() function displays output
• Multiple ways to format output:
• Basic print: print("Hello World")
• Using f-strings: print(f"Hello {name}")
• Using .format(): print("Hello {}".format(name))
• Using % operator: print("Hello %s" % name)
Arithmetic Operators
• Addition (+): \(a + b\)
• Subtraction (-): \(a - b\)
• Multiplication (*): \(a * b\)
• Division (/): \(a / b\)
• Floor Division (//): \(a // b\)
• Modulus (%): \(a % b\)
• Exponentiation (**): \(a ** b\)
Comparison Operators
• Equal to (==)
• Not equal to (!=)
• Greater than (>)
• Less than (<)
• Greater than or equal to (>=)
• Less than or equal to (<=)
Logical Operators
• and: Returns True if both statements are true
• or: Returns True if one of the statements is true
• not: Reverses the result, returns False if true
Assignment Operators
• Simple assignment (=)
• Add and assign (+=)
• Subtract and assign (-=)
• Multiply and assign (*=)
Divide and assign (/=)
Examples of compound assignments
Introduction to Strings
• Sequence of characters
• Created using single or double quotes
Immutable data type
Example: text = "Hello, Python!"
String Operations
• Concatenation using + operator
• Repetition using * operator
• String indexing [index]
• String slicing [start:end:step]
• Length using len() function
String Methods
• upper() and lower()
• strip(), lstrip(), rstrip()
• replace()
• split() and join()
• find() and index()
String Formatting
• Using % operator
• str.format() method
f-strings (formatted string literals)
Example of each formatting method
Best practices for string formatting
Common String Applications
• Input validation
• Text processing
• File operations
• Data parsing
• User interface development
Practice Exercises
• Write a program to get user input and display it
• Create a calculator using arithmetic operators
• Compare two numbers using comparison operators
• Perform string manipulations using different methods
• Format output using various string formatting techniques