Python Notes
Python Notes
o Syntax refers to the set of rules and structure that define how Python code should be written.
o It includes things like the order of statements, the use of punctuation, and the indentation of code
blocks.
o In Python, the syntax is designed to be readable and easy to understand.
o It uses indentation (usually four spaces) to define code blocks instead of using braces or keywords
like other programming languages. This indentation is important as it helps Python identify the
beginning and end of blocks of code.
o For example, consider the following code snippet:
# Example of syntax in Python
x=5
if x > 0:
print("Positive number")
else:
print("Non-positive number")
In this code, the syntax is as follows:
- The variable `x` is assigned the value of `5`.
- The `if` statement checks if `x` is greater than `0`.
- The subsequent indented block of code executes if the condition is true, printing "Positive number".
- The `else` statement is followed by another indented block of code, which executes if the condition is false,
printing "Non-positive number".
o Python relies heavily on indentation to structure code and make it readable.
o Proper indentation ensures that the code is visually organized and helps Python understand the
logical structure of the program.
o Practice problem:
Write a Python program that asks the user to enter two numbers and calculates their sum.
o Here's a possible solution:
In this code, we start by creating an array `my_array` using the `array` function from the `array` module.
The elements of the array are initialized from a list of numbers.Then, the user is prompted to enter a number
to check. The input is stored in the `target` variable. We use the `in` operator to check if the `target` exists
in the array, and an appropriate message is displayed based on the result.
3. Tuples:
Tuples are similar to lists but are immutable, meaning their elements cannot be changed after
creation.
They are defined using parentheses ().
Tuples are typically used to store related pieces of data together and protect them from being
modified accidentally.
4. Dictionaries:
Dictionaries are unordered collections of key-value pairs enclosed in curly braces ({key: value}).
They are used to store and retrieve data based on unique keys instead of numerical indices.
Dictionaries are mutable, and values can be modified, added, or deleted based on their keys.
They are useful for representing structured data and mapping relationships between different
pieces of information.
5. Sets:
Sets are unordered collections of unique elements enclosed in curly braces ({element1, element2}).
They can be used to perform mathematical set operations like union, intersection, and difference.
Sets do not allow duplicates, and their elements are not accessed using indices.
Sets are mutable, and elements can be added or removed.
6. Strings:
While not strictly a data structure, strings are sequences of characters and can be treated as a
collection of individual elements.
Strings have their own set of methods for manipulation, searching, and formatting.
Practice problems
Easy-Level Problems:
1. Problem: Write a Python function that takes a string as input and prints each character of the string on a
separate line.
2. Problem: Write a Python program that prompts the user to enter a positive integer and checks if it is a
prime number. Display an appropriate message.
3. Problem: Write a Python program that calculates and displays the sum of all odd numbers from 1 to 100.
Intermediate-Level Problems:
1. Problem: Write a Python function that takes a list of numbers as input and returns a new list containing
only the even numbers from the original list.
2. Problem: Write a Python program that prompts the user to enter a sentence and counts the occurrences of
each word in the sentence. Display the word count for each unique word.
3. Problem: Write a Python program that generates and prints the Fibonacci sequence up to a given number.
Prompt the user to enter the maximum number.
Hard-Level Problems:
1. Problem: Write a Python function that takes a list of integers as input and returns the two numbers with
the smallest absolute difference. If multiple pairs have the same smallest difference, return any one of them.
2. Problem: Write a Python program that prompts the user to enter a sentence and finds the longest word in
the sentence. If multiple words have the same length, return any one of them.
3. Problem: Write a Python program that simulates the game of Tic-Tac-Toe between two players. Prompt
the players for their moves and determine the winner or if the game ends in a draw.