Docsity

Introduction to Python Programming: Variables, Data Types, and Operators, Study notes of Computer science

Partial preview of the text

Download Introduction to Python Programming: Variables, Data Types, and Operators and more Study notes Computer science in PDF only on Docsity!

Introduction to Python

  • A program is a set of instructions for a computer.
  • A programming language is used to write these

instructions (e.g., Python, C, C++, Java).

  • Python, created by Guido van Rossum in 1991, is a

popular and easy-to-learn language.

  • It is widely used in software development, web

development, data science, and scientific computing.

Shilpa Bafna(P.G.T)(IP)

1. Interactive Mode

  • Used to test single-line code quickly.
  • Code is not saved; must be retyped to run again. Modes of Python Shilpa Bafna(P.G.T)(IP)

Shilpa Bafna(P.G.T)(IP) Shilpa Bafna(P.G.T)(IP)

Shilpa Bafna(P.G.T)(IP)

Shilpa Bafna(P.G.T)(IP) In Python, we use assignment statements to create variables and store values like text, numbers, or a mix of both. Example: gender = 'M’ message = "Keep Smiling“ price = 987. Here, gender, message, and price are variables. A variable must be assigned a value before it is used, otherwise the program will show an error. The interpreter replaces the variable name with its value during execution.

Variable

DATA TYPES

Sequence A sequence in Python is an ordered collection of items indexed by numbers. The main sequence types are:

  • String → Group of characters enclosed in ' ' or " ". Example: str1 = 'Hello Friend’ str2 = "452" (Cannot do math on strings, even if they look like numbers.)
  • List → Items of different types enclosed in [ ]. Example:list1 = [5, 3.4, "New Delhi", "20C", 45]
  • Tuple → Items enclosed in ( ). Similar to lists but cannot be changed once created. Example: tuple1 = (10, 20, "Apple", 3.4, 'a')

Mapping In Python, Dictionary is the standard mapping type. It stores data as key–value pairs inside { }. Keys are usually strings; values can be any type. Access values using their key. Example: student = {"name": "Ravi", "age": 16} print(student["name"]) # Ravi

Identify the operators

* /^ %

// >^ =

ARITHMETIC OPERATORS

WHAT IF ALL THE OPERATORS ARE IN ONE

STATEMENT

???

  • 5 + 6 / 3 * 4 // 2 ** 3
  • 8+2-3//2**