0% found this document useful (0 votes)
3 views

Python_2

The document provides a guide to Python's basic syntax, data types, and arithmetic operations. It covers the structure of Python code, the use of variables, and different data types such as integers, floats, strings, and booleans. Additionally, it includes practical exercises for applying these concepts and encourages experimentation in the Python interactive shell.

Uploaded by

infinitein093
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_2

The document provides a guide to Python's basic syntax, data types, and arithmetic operations. It covers the structure of Python code, the use of variables, and different data types such as integers, floats, strings, and booleans. Additionally, it includes practical exercises for applying these concepts and encourages experimentation in the Python interactive shell.

Uploaded by

infinitein093
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Day 2: Basic Syntax & Data Types

Today you'll dive into Python’s fundamental building blocks by learning about variables, data
types, and basic operations. Follow this detailed plan and complete the exercises to solidify your
understanding.

Step 1: Understanding Python’s Basic Syntax

What to Know:

• Syntax & Structure:


Python code is written in plain text and uses indentation (usually 4 spaces) to define code
blocks rather than curly braces ({}) like in some other languages.

• Statements:
Each line of code is a statement. Unlike some languages, you don’t need to end statements
with a semicolon.

• Comments:
Use the hash symbol (#) for comments. Anything following # on that line is ignored by
Python.

Quick Example:

# This is a comment

print("Hello, Python!") # This prints a message to the console

Step 2: Working with Variables and Data Types

A. Variables

• Definition:
Variables store data values. In Python, you don’t need to declare the type; it’s determined
automatically.

• Assignment:
Use the equals sign (=) to assign values to variables.

B. Data Types

1. Integers (int): Whole numbers.

age = 30

2. Floating-Point Numbers (float): Numbers with decimals.

pi = 3.14159

3. Strings (str): Text enclosed in quotes.

name = "Alice"

4. (Optional) Booleans (bool): Represent truth values: True or False.


is_student = True

Practice:

• Open your code editor (or interactive Python shell) and type the following:

# Variables and data types demonstration

# Integer

number = 10

print("The integer is:", number)

# Float

fraction = 3.14

print("The float is:", fraction)

# String

greeting = "Hello, World!"

print("The string is:", greeting)

# Boolean

status = False

print("The boolean value is:", status)

Save your file as day2_variables.py and run it to see the outputs.

Step 3: Basic Arithmetic Operations

Operations to Explore:

• Addition: +

• Subtraction: -

• Multiplication: *

• Division: /

• Exponentiation: **

• Modulus (Remainder): %
Example:

a = 15

b=4

print("Addition:", a + b) # 19

print("Subtraction:", a - b) # 11

print("Multiplication:", a * b) # 60

print("Division:", a / b) # 3.75

print("Exponentiation:", a ** b) # 15 raised to the power of 4

print("Modulus:", a % b) # Remainder of 15 divided by 4

Practice:

• Create a new file named day2_arithmetic.py.

• Write a script that:

1. Assigns two numbers to variables.

2. Computes and prints the results of each of the arithmetic operations.

• Challenge: Write a program to calculate the area of a rectangle.


Hint: Use two variables for width and height, then multiply them.

Step 4: Experiment in the Python Interactive Shell

1. Open the Terminal or Command Prompt.

2. Start Python's interactive shell by typing:

python

3. Try the following commands one by one:

# Simple arithmetic in the shell

print(7 + 3)

x = 10

y = 5.5

print("x:", x, "and y:", y)

message = "Learning Python is fun!"

print(message)

4. Exit the shell:


Type exit() and press Enter.

You might also like