ЁЯРН

Python Lecture: Introduction and Basics

Jul 16, 2024

Python Lecture: Introduction and Basics

Introduction

  • Python is a versatile language used in web development (e.g., Django), machine learning, data science, etc.
  • The course is designed for beginners; no prior coding knowledge is required.
  • Python is known for being a loved, easy-to-learn language in the tech industry.

Coding Environment

  • Use pythonanywhere.com to write and execute Python code.
  • Python code must be written in a language the computer understands (e.g., Python, Java, etc.), as computers do not comprehend human languages like Hindi or English.
  • To translate Python code to machine level, interpreters and compilers are used.

Motivation for Learning Python

  • Python is simple and easy to understand, making it popular among beginners and experienced programmers alike.
  • Pythonic skills open pathways to various fields like AI, ML, data science, and web development.

Setup and Tools

  • Use Visual Studio Code (VS Code) as the code editor.
  • Install Python from python.org.
  • Set up Python and VS Code for coding.

Basic Coding in Python

  • Write and execute simple Python programs using the print() function.
  • Understand the concept of variables and data types (int, str, float, bool, None).

Writing a Simple Program

print("Hello, World!") # simple print statement
  • Combining strings and variables in print statements.

Variables and Data Types

  • Variables store data that can change during program execution.
  • Basic data types include integers (int), strings (str), floating-point numbers (float), and booleans (bool).
  • Use type() function to check the data type of a variable.

Arithmetic Operations

  • Basic operations: addition (+), subtraction (-), multiplication (), division (/), modulus (%), exponentiation () etc.

Relational and Logical Operators

  • Relational operators: ==, !=, >, <, >=, <=
  • Logical operators: and, or, not

Type Conversion

  • Implicit and explicit type conversion (casting).
    • Implicit: Python automatically converts data types.
    • Explicit: Force conversion using int(), float(), str(), etc.

User Input

  • Use input() function to take user input, which is always of type str by default.
  • Convert input to other types as needed using type casting.

Comments in Python

  • Use comments to describe code: # for single-line comments and """ or ''' for multi-line comments.

Practical Examples

Example 1: Input Two Numbers and Print Their Sum

first = int(input("Enter first number: ")) second = int(input("Enter second number: ")) sum = first + second print("Sum: ", sum)

Example 2: Input Side of a Square and Print Its Area

side = float(input("Enter side of the square: ")) area = side * side print("Area: ", area)

Example 3: Input Two Floating Point Numbers and Print Their Average

first = float(input("Enter first number: ")) second = float(input("Enter second number: ")) average = (first + second) / 2 print("Average: ", average)

Example 4: Compare Two Integers

first = int(input("Enter first number: ")) second = int(input("Enter second number: ")) print(first >= second)

Conclusion

  • Practice these basic concepts to build a strong foundation in Python.
  • Additional resources and slides can be found in the description for further learning.
  • Keep practicing to improve your programming skills.

Next lecture will cover advanced topics and deeper dive into Python programming.