You are on page 1of 5

CHAPTER 1

1. What is programming?
 Programming is the process of creating programs using programming languages to solve problems
and automate tasks.
 Humans uses human languages like Hindi, English, etc.
 For communicating with computers we will use programing languages like python, java, etc.
 Computers don't understand programming languages directly. They only understand machine
language, which is a language that is made up of binary code (1s and 0s).
 When a programmer writes code in a programming language, the code needs to be translated into
machine language so that the computer can execute it. This is done using a compiler or interpreter,
which translates the code into machine language that the computer can understand and execute.
 Example: A programmer might create a program that can help a company keep track of inventory or
a program that can simulate the behavior of a complex system.
2. Python is—
 High-level, open-source programming language.
 Clear syntax and easy to learn and use.
 Widely used for web development, scientific computing, and machine learning.
 Uses dynamic typing and is an interpreted language.
 Large standard library with pre-written code for various purposes.
 Popular in data science and machine learning due to its ease of use and flexibility.
3. Variables:
 In computer programming, a variable is a named location in memory that is used to store a value. It
is a container that holds a data value or a reference to a data value.
 In Python, variables are created when you assign a value to them using the assignment operator
(=). For example:
 x=5
 In this example, x is a variable that has been assigned the value 5.
 Variables can store different types of data, such as:
o Numbers (integers, floating-point numbers)
o Strings (text)
o Booleans (True or False values)
o Lists (ordered collections of items) eg:
x = [1, 2, "three", 4.0, True]
o Dictionaries (unordered collections of key-value pairs)
X = {"name": "Alice", "age": 30, "city": "New York"}
o Tuples (immutable ordered collections of items)
x = (1, 2, "three", 4.0, True)
o Sets (unordered collections of unique items)
x = {1, 2, 3, 4, 5}
 Rules for variables:
o A variable name must start with a letter or underscore (_) and cannot start with a number.
This means that the first character of a variable name cannot be a number.
Eg: x = 10 x_1 = 11 _my_var = "Hello" but not 123var = 10 1_number = 20
o A variable name can only contain letters, numbers, and underscores.
o Variable names are case-sensitive, a = 1 is different from A = 1
o Avoid using reserved keywords, if, else, etc.
o Space is not allowed.
4. Exercise 1
i. Write a program that prints the phrase "Hello, world!" to the console.
ii. Write a program that prints the phrase "I love Python!" to the console.
iii. Print the result of adding 5 and 3 to the console.
iv. Print the result of subtracting 7 from 10 to the console.
v. Print the result of multiplying 2 and 4 to the console.
vi. Print the result of dividing 10 by 3 to the console.
vii. Print the remainder of 20 divided by 7 to the console.
viii. Print the quotient of 10 divided by 3.
ix. Write a program that prompts the user to enter two numbers and then adds them together.
x. Write a program that prompts the user to enter two numbers and then multiply them togeather.
xi. Write a program that prompts the user to enter two numbers and then subtract them together.
xii. Write a program that prompts the user to enter two numbers and then divide them together.
xiii. Write a program that prompts the user to enter two numbers and then print the quotient of those.
xiv. Write a program that prompts the user to enter two numbers and then print the reminder of those.
xv. Write a program that prompts the user to enter a number, then calculates and prints the square of
that number.
xvi. Write a program that prompts the user to enter a number, then calculates and prints the cube of
that number.
xvii. Write a program that prompts the user to enter the radius of a circle and then calculates and prints
its area.
xviii. Write a program that prompts the user to enter the any 2 numbers and then calculates and print its
average.
xix. Write a program that prompts the user to enter the length and width of a rectangle and then
calculates and prints its perimeter.
xx. Write a program that prompts the user to enter the price of an item and the sales tax rate, and
then calculates and prints the total cost with tax.
xxi. Write a program that prompts the user to enter a number of seconds, then converts and prints the
number of seconds into minutes and seconds.
xxii. Write a program that prompts the user to enter the temperature in Fahrenheit, then converts and
prints the temperature in Celsius.
xxiii. Write a program that prompts the user to enter a length in inches, then converts and prints the
length in centimeters.
xxiv. Write a program that prompts the user to enter a weight in pounds, then converts and prints the
weight in kilograms.
xxv. Write a program that prompts the user to enter a distance in miles, then converts and prints the
distance in kilometers.
5. Here are some conditional statements in Python:
 if statement
 elif statement (short for "else if")
 else statement
 Ternary operator (also known as conditional operator)
 assert statement
 try/except statement (for handling exceptions)
 while loop (for looping until a condition is no longer true)
 for loop (for looping over an iterable, such as a list or range)
 in keyword (for checking if an element is in an iterable)
 not keyword (for negating a boolean expression)
6. Here is a list of comparison operators in Python:

 Equal to: ==
 Not equal to: !=
 Greater than: >
 Less than: <
 Greater than or equal to: >=
 Less than or equal to: <=
These operators are used to compare two values and return a Boolean value (True or False) based on the
result of the comparison.
7. Here is a full list of logical operators in Python:
 and: Returns True if both expressions are True
 or: Returns True if at least one expression is True
 not: Reverses the logical state of the expression (True becomes False, False becomes True)
These operators can be used to combine multiple conditions in a single statement.
8. Python functions:
 print() - prints output to the console.
 input() - accepts user input from the console.
 len() - returns the length of an iterable object.
 type() - returns the type of an object.
 int() - converts a value to an integer.
 float() - converts a value to a floating-point number.
 str() - converts a value to a string.
 list() - converts an iterable object to a list.
 tuple() - converts an iterable object to a tuple.
 set() - converts an iterable object to a set.
 dict() - creates a new dictionary object.
 range() - generates a sequence of numbers.
 sum() - returns the sum of an iterable object.
 max() - returns the maximum value of an iterable object.
 min() - returns the minimum value of an iterable object.
 abs() - returns the absolute value of a number.
 round() - rounds a number to a specified number of decimal places.
 sorted() - returns a sorted list from an iterable object.
 reversed() - returns a reversed iterator from an iterable object.
 zip() - returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the
argument sequences or iterables.
 any() - returns True if any element in an iterable object is true, otherwise returns False.
 all() - returns True if all elements in an iterable object are true, otherwise returns False.
 format() - formats a string using replacement fields and values.
 join() - joins the elements of an iterable object into a string.
 split() - splits a string into a list of substrings based on a delimiter.
 strip() - returns a string with whitespace stripped from both ends.
 replace() - replaces occurrences of a substring in a string with another substring.
 count() - returns the number of occurrences of a substring in a string.
 isalpha() - returns True if all characters in a string are alphabetic, otherwise returns False.
 isdigit() - returns True if all characters in a string are digits, otherwise returns False.
 isalnum() - returns True if all characters in a string are alphanumeric, otherwise returns False.
 isspace() - returns True if all characters in a string are whitespace, otherwise returns False.
 capitalize() - returns a string with the first character capitalized and the rest in lowercase.
 title() - returns a string with the first character of each word capitalized and the rest in lowercase.
 lower() - returns a string in lowercase.
 upper() - returns a string in uppercase.
 find() - returns the index of the first occurrence of a substring in a string, or -1 if not found.
 index() - returns the index of the first occurrence of a substring in a string, or raises an error if not
found.
 startswith() - returns True if a string starts with a specified substring, otherwise returns False.
 endswith() - returns True if a string ends with a specified substring, otherwise returns False.
 enumerate() - returns an iterator of tuples, where the first element of each tuple is an index and
the second is the corresponding element from an iterable object.
 map() - applies a function to each element of an iterable object and returns an iterator of the
results.
 filter() - returns an iterator of the elements of an iterable object for which a given function returns
True.
 reduce() - applies a function to the first two elements of an iterable object, then applies the
function to the result and the next element
 str.format(): This method is used to format a string with specified values.\
 str.replace(): This method is used to replace a specified substring in a string with another substring.
 str.upper(): This method is used to convert a string to uppercase.
 str.lower(): This method is used to convert a string to lowercase.
 str.strip(): This method is used to remove leading and trailing whitespace from a string.
9. Exercise 2
i. Write a program that prompts the user to enter an integer and then checks if it is even or odd. If it
is even, print "The number is even". If it is odd, print "The number is odd".
ii. Write a program that prompts the user to enter their age and checks if they are eligible to vote or
not. If they are 18 or older, print "You are eligible to vote". If they are younger than 18, print "You
are not eligible to vote".
iii. Write a program that prompts the user to enter their name and password. If the name is "Alice"
and the password is "swordfish", print "Access granted". If the name or password is incorrect, print
"Access denied".
iv. Write a program that prompts the user to enter three integers and then finds and prints the largest
one.
v. Write a program that prompts the user to enter two numbers and checks if they are equal or not. If
they are equal, print "The numbers are equal". If they are not equal, print "The numbers are not
equal".
vi. Write a program that prompts the user to enter their name and age, and then prints out a message
that says "Hello, [name]! You are [age] years old."
vii. Write a program that prompts the user to enter a year. The program should then print out whether
that year is a leap year or not.
viii. Write a program that prompts the user to enter a year and a month, and then prints out the
number of days in that month (taking into account leap years).
ix. Write a program that prompts the user to enter their exam score and prints out their
corresponding letter grade based on the following scale: A (90-100), B (80-89), C (70-79), D (60-69),
F (0-59).
x. Write a program that prompts the user to enter a number and then prints out the sum of all the
numbers from 1 to that number.
xi. Write a program that takes a number as input and prints whether it is positive, negative or zero.
xii. Write a program that takes a character as input and prints whether it is a vowel or a consonant.
xiii. Write a program that prompts the user to enter a number between 1 and 7, and then prints out the
corresponding day of the week (1 for Sunday, 2 for Monday, etc.).
xiv. Write a program that prompts the user to enter a 5 letter string and then prints the first and
second and last characters of the string.
xv. Write a program that prompts the user to enter a string and then prints the first and last characters
of the string.
xvi. Write a program that prompts the user to enter a string and then prints the string without the first
and last characters.
xvii. Write a program that prompts the user to enter a sentence and then prints the sentence in reverse
order.
xviii. Write a program that prompts the user to enter a string and then prints every other character in
the string.

You might also like