Skip to content

Python Programming · Week 2 notes

Variables

Python Week 2: Overview

This lesson introduces key concepts regarding variables and their role in storing information.

Key Topics Covered:

  • Introduction to Variables
  • Data Types: String, Integer, Float
  • Declaring and Redefining Variables
  • Operations on Integer and Float Variables
  • String Concatenation

Definitions:

Variables:

Variables are used to store information in memory. They can store different types of data, such as numbers, text, and lists. In Python, variables are created when you assign a value to them.

Data Types:

Data types represent the kinds of information you can store in variables. Common data types include:

  • String: A sequence of characters (text). Strings are enclosed in quotes (e.g., "Hello").
  • Integer: Whole numbers without a decimal point (e.g., 5, -10).
  • Float: Numbers with a decimal point (e.g., 3.14, -0.01).

Operations:

You can perform mathematical operations on variables using operators like +, -, *, and /.

Concatenation:

Concatenation is the process of joining two strings together using the + operator.

Quick check

  1. What is a variable in Python?

  2. Which of the following is an integer?

  3. What is the result of 'Hello' + ' ' + 'World'?

Data Types

Understanding Data Types in Detail

In this video, we dive deeper into the different data types in Python and how to use them effectively.

Strings

Strings are used to represent text. You can use single or double quotes to create strings.

name = "Alice" greeting = 'Hello'

Integers and Floats

Integers are whole numbers, while floats are numbers with decimal points. Python handles these types differently in memory.

age = 25 # Integer height = 5.9 # Float

Type Conversion

You can convert between data types using functions like str(), int(), and float().

string_num = "123" converted_num = int(string_num) print(converted_num + 5) # Output: 128

Quick check

  1. What function converts a number to a string?