Overview
This lecture covers the basic introduction to the Python language, syntax, variables, data types, operators, input-output, type conversion, and some basic practice questions.
Introduction to Python
- Python is a simple, open-source, high-level, and versatile programming language.
- Coders use Python in web development, machine learning, data science, etc.
- Python is easy to learn and looks like English.
- An interpreter is used to translate Python into machine-level language.
Python Installation and Setup
- Python can be downloaded from python.org; check the necessary boxes during installation.
- Check the version on both Windows/Mac using the command python --version.
- Use Visual Studio Code (VS Code) editor for writing code.
Basic Concepts
- Meaning of programming: giving instructions to the machine, called code.
- Process: Code (high-level language) тЖТ Translator тЖТ Machine language (0,1)
- Character set: English letters, digits (0-9), special symbols, whitespace are valid.
Variables and Data Types
- Variables: the name of a memory location where data is stored.
- Assignment: variable = value like age = 23
- Data types:
- int (integer): whole numbers (like 5, -5, 0)
- float: decimal numbers (like 4.5)
- str (string): words or sentences (like "Python")
- bool (boolean): True/False
- None: no value
Variable Naming Rules
- Names can include letters, digits, underscore (_); digits cannot start the name.
- Special symbols and keywords (like True, None, False) cannot be used in variable names.
- Keep variable names simple, short, and meaningful._
Keywords and Case Sensitivity
- Python has fixed keywords with fixed meanings.
- Python is case sensitive; name and Name are considered different.
Output (print) and Input (input)
- print() is used to display output on the screen.
- input() takes data from the user; input always takes a string, so type casting is necessary.
- Type casting: int(input()), float(input()), str(val), etc.
Operators
- Arithmetic operators: +, -, *, /, %, **
- Comparison operators: ==, !=, >, <, >=, <=
- Assignment operators: =, +=, -=, *=, /=, %=, **=
- Logical operators: not, and, or
Comments
- Use # for single-line comments, ''' or """ for multi-line comments.
- Comments explain the code and do not run.
Type Conversion and Casting
- Python automatically converts int to float (implicit conversion).
- Manual type casting is done using int(), float(), str().
Practice Questions
- Take two numbers as input and print their sum.
- Take the side of a square as input and print its area.
- Take two floating-point numbers as input and print their average.
- Take two integers as input and print True if the first is greater than or equal to the second, otherwise False.
Key Terms & Definitions
- Variable тАФ the name of memory where the value is stored.
- Data Type тАФ the type of data stored in a variable; int, float, str, bool, None.
- Keyword тАФ reserved words that cannot be used as variable names.
- Type Casting тАФ converting one type to another.
- Operator тАФ a symbol that performs operations on values (like +, -).
- Comment тАФ used to explain code, does not run.
Action Items / Next Steps
- Install VS Code and Python.
- Write and run basic programs (sum, area, average) yourself.
- Download lecture notes/slides from the description.
- Explore for the next lecture: Conditional Statements (if-else).