INTRODUCTION
TO PYTHON AND
PROGRAMMING
PYTHON CLASSES | 28 JAN 2024
Welcome to Python Programming!
Topics will be covered today:
- Introduction to Python
- Python Syntax, Variables, and Data Types
- Input/Output Operations
Why Python is Awesome
- High-level, versatile programming language.
- Widely used in web development, data science, AI, scripting, and more.
- Easy-to-learn syntax, perfect for beginners.
- Huge community and plenty of resources.
What is Programming?
- Telling a computer what to do using instructions.
- Programming helps automate tasks and solve problems.
- Think of it like giving step-by-step directions to a robot!
Python in a Nutshell
- General-purpose, high-level programming language.
- Focuses on readability and simplicity.
Example of Pythonʼs simplicity:
Setting up Python
1. Install Python from python.org/downloads .
2. Use an editor like VS Code, PyCharm and etc.
Variables in Python
What Are Variables?
Variables store data that your program can use.
Naming rules:
- Must start with a letter or underscore.
- Canʼt use spaces or special characters.
- Case-sensitive.
Data Types
Common Data Types in Python
- Basic data types:
- int (e.g., 10
- float (e.g., 3.14
- str (e.g., "Hello")
- bool (e.g., True/False)
Input and Output
Input: Getting data from the user.
Output: Displaying information.
Combine input and output for interaction.
Print Function
Purpose: Used to display messages or variable values in the console.
1. Print a simple message:
2. Print multiple items:
3. Use formatted strings (f-strings):
The input() Function: Interacting with Users
Purpose: Allows the program to accept user input.
1. Simple input:
2. Using numbers with int or float casting:
input() always returns a string, so you may need to convert it for numbers.
Provide clear prompts to guide users.
The input() Function: Interacting with Users
Purpose: Allows the program to accept user input.
1. Simple input:
2. Using numbers with int or float casting:
input() always returns a string, so you may need to convert it for numbers.
Provide clear prompts to guide users.
Type Casting: Converting Between Data Types
Purpose: Converts one data type to another (e.g., string to integer, float to integer).
Common casting functions:
int() - Converts to integer.
float() - Converts to floating-point number.
str() - Converts to string.
Common Errors
Errors are part of the learning process.
- Examples of common errors:
- SyntaxError: Missing indentation or quotes.
- NameError: Using an undefined variable.
- Tips to debug:
- Read the error message carefully.
- Check your syntax and spelling.
Letʼs write our first program
Simple program to:
1. Ask for the userʼs name.
2. Ask for their birth year.
3. Calculate their age.
More Exercises
Letʼs Practice!
Task 1 Write a program that asks for two numbers and prints their sum.
Task 2 Create a program that greets the user with their name.
Task 3 Write a program to convert Celsius to Fahrenheit:
Some materials to read
1. Python print() Function
2. Python input() function
3. Python Casting