Python Basics for Beginners

Lecture 1.2: Python Basics

Introduction

The fundamentals of Python programming, including knowledge of variables, data types, and standard operations, will be covered in this course. You will be able to build Python code that handles data well and carries out basic calculations by the end of this lesson.

  1. Variables and Data Types in Python:

What is a Variable?

A variable is a container for storing data values. In Python, variables are created the moment you assign a value to them.

# Example:
x = 5       # x is a variable that holds the integer value 5
name = "Alice"  # name is a variable that holds the string value "Alice"

Common Data Types in Python:

Python supports various data types that are useful for different kinds of data handling:

  1. Integers (int): Whole numbers, positive or negative.
  2. Floating Point (float): Numbers with decimal points.
  3. Strings (str): Sequence of characters.
  4. Booleans (bool): Represents either True or False.
# Example of different data types:
age = 25        # Integer
height = 5.7    # Float
name = "Bob"    # String
is_student = True  # Boolean

2. Operators in Python:

Operators are used to perform operations on variables and values. Here’s a breakdown of the key types of operators:

 

Arithmetic Operators:

These are used for basic mathematical operations:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
# Example:
a = 10
b = 3
print(a + b)  # Output: 13
print(a - b)  # Output: 7
print(a * b)  # Output: 30
print(a / b)  # Output: 3.3333

Comparison Operators:

These operators compare two values and return a Boolean (True or False)

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
# Example:
x = 5
y = 8
print(x == y)  # Output: False
print(x < y)   # Output: True

Logical Operators:

These are used to combine conditional statements:

  • and : Returns True if both statements are true
  • or : Returns True if at least one of the statements is true
  • not : Reverses the result, returns False if the result is true
# Example:
a = True
b = False
print(a and b)  # Output: False
print(a or b)   # Output: True
print(not a)    # Output: False

3. Input/Output and Comments in Python:

Input in Python

You can take input from users using the input() function. It returns the input as a string, but you can convert it to other data types if needed.

# Example of input:
name = input("Enter your name: ")
age = int(input("Enter your age: "))  # Convert input to integer
print(f"Hello, {name}. You are {age} years old.")

Output in Python:

The print() function is used to display output.

# Example of output:
print("Hello, World!")

You can also format your output to include variables:

name = "Alice"
print(f"Hello, {name}")  # Using f-string for formatting

Comments in Python:

Comments are lines in your code that are not executed by Python. They are useful for explaining your code and making it more readable. Use the # symbol for single-line comments.

# This is a comment
x = 5  # This is an inline comment

For multi-line comments, use triple quotes (''' or """):

'''
This is a multi-line comment.
It spans multiple lines.
'''

Below is the content for Lecture 1.2: Python Basics designed as a course section to post on your website. It covers Variables and Data Types, Operators, and Input/Output and Comments in Python, and can be posted as part of your Python for Data Science course.


Lecture 1.2: Python Basics

Introduction

In this lecture, we will cover the basics of Python programming, which includes understanding variables, data types, and common operations. By the end of this lecture, you’ll be able to write Python code that handles data efficiently and performs simple computations.


1. Variables and Data Types in Python

What is a Variable?

A variable is a container for storing data values. In Python, variables are created the moment you assign a value to them.

pythonCopy code# Example:
x = 5       # x is a variable that holds the integer value 5
name = "Alice"  # name is a variable that holds the string value "Alice"

Common Data Types in Python

Python supports various data types that are useful for different kinds of data handling:

  • Integers (int): Whole numbers, positive or negative.
  • Floating Point (float): Numbers with decimal points.
  • Strings (str): Sequence of characters.
  • Booleans (bool): Represents either True or False.
pythonCopy code# Example of different data types:
age = 25        # Integer
height = 5.7    # Float
name = "Bob"    # String
is_student = True  # Boolean

2. Operators in Python

Operators are used to perform operations on variables and values. Here’s a breakdown of the key types of operators:

Arithmetic Operators

These are used for basic mathematical operations:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
pythonCopy code# Example:
a = 10
b = 3
print(a + b)  # Output: 13
print(a - b)  # Output: 7
print(a * b)  # Output: 30
print(a / b)  # Output: 3.3333

Comparison Operators

These operators compare two values and return a Boolean (True or False):

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
pythonCopy code# Example:
x = 5
y = 8
print(x == y)  # Output: False
print(x < y)   # Output: True

Logical Operators

These are used to combine conditional statements:

  • and : Returns True if both statements are true
  • or : Returns True if at least one of the statements is true
  • not : Reverses the result, returns False if the result is true
pythonCopy code# Example:
a = True
b = False
print(a and b)  # Output: False
print(a or b)   # Output: True
print(not a)    # Output: False

3. Input/Output and Comments in Python

Input in Python

You can take input from users using the input() function. It returns the input as a string, but you can convert it to other data types if needed.

pythonCopy code# Example of input:
name = input("Enter your name: ")
age = int(input("Enter your age: "))  # Convert input to integer
print(f"Hello, {name}. You are {age} years old.")

Output in Python

The print() function is used to display output.

pythonCopy code# Example of output:
print("Hello, World!")

You can also format your output to include variables:

pythonCopy codename = "Alice"
print(f"Hello, {name}")  # Using f-string for formatting

Comments in Python

Comments are lines in your code that are not executed by Python. They are useful for explaining your code and making it more readable. Use the # symbol for single-line comments.

pythonCopy code# This is a comment
x = 5  # This is an inline comment

For multi-line comments, use triple quotes (''' or """):

pythonCopy code'''
This is a multi-line comment.
It spans multiple lines.
'''

Conclusion:

This concludes the basics of Python covering variables, data types, operators, input/output, and comments. In the next lecture, we’ll explore control flow statements in Python, which will allow you to make decisions and perform repeated actions in your code.

One thought on “Python Basics for Beginners

Leave a Reply

Your email address will not be published. Required fields are marked *