Intro2Python
Intro2Python
nd
Python Training
Installing Jupyter Notebook
jupyter notebook
Basic data types and variables
Data Types are used to define the type of a variable, indicating the kind
of data that will be stored.
Different types of data can be stored in memory, such as numeric
values, alphanumeric characters, and more.
var2 = 10
var3 = 10.023
Numeric Type Examples
• Strings are sequences of characters enclosed within either single (' ')
or double (" ") quotes.
• They represent textual data and can include letters, digits, and
special characters.
• Python allows for the use of various operations and manipulations
on strings.
String Indexing and Slicing:
Strings can be indexed and sliced using the square brackets [ ] and the
slicing notation [:]. Indexing starts from 0 for the first character and can
also be negative for backward indexing.
Hello World!
H
lo
llo World!
Hello World!Hello World!
Hello World!TEST
List Data Type:
# Concatenating lists
new_list = my_list + [5, 6]print(new_list)
Output:
10
Hello
2
['Hello', 3.14]
[10, 'Hello', 3.14, [1, 2, 3], 5, 6]
Tuple Data Type:
• Tuples are similar to lists but are enclosed in parentheses ( ).
10
World
9
range() Function:
01234
234567
13579
Dictionaries:
• Python dictionaries are a type of hash table that consists of
key-value pairs.
• They work like associative arrays or hashes found in other
programming languages.
• A dictionary's key can be almost any Python type, but they
are usually numbers or strings. The corresponding values can
be any arbitrary Python objects.
• Dictionaries are enclosed within curly braces {} and values
can be assigned and accessed using square brackets [].
# Creating a dictionary
person = {
"first_name": "John",
"last_name": "Doe",
"age": 30,
"city": "New York“
}
# Accessing values using keys
print("First Name:", person["first_name"])
print("Age:", person["age"])
# Modifying values
person["age"] = 31print("Updated Age:", person["age"])
# Adding new key-value pairs
person["occupation"] = "Engineer“ # Iterating through keys and
# Deleting a key-value pair values
del person["city"] for key, value in person.items():
print(key + ":", value)
# Checking if a key exists
if "occupation" in person: # Clearing the dictionary
print("Occupation:", person["occupation"]) person.clear()
Output:
number = 42
# Using comparison operators is_positive = bool(number > 0)
x=5 is_even = bool(number % 2 == 0)
y = 10
greater_than = x > y print("Is the number positive?", is_positive)
print("Is the number even?", is_even)
less_than = x < y
print("Is x greater than y?",
greater_than)
print("Is x less than y?", less_than)
Output:
Is it sunny? True
Is it raining? False
Is x greater than y? False
Is x less than y? True
Is the number positive? True
Is the number even? True
Data Type Conversion Functions:
You can convert between different data types using built-in functions. These
functions allow you to change the type of a variable to another type.
The most commonly used data type conversion functions are:
• int() - converts to an integer
• float() - converts to a floating-point number
• str()- converts to a string
• bool() - converts to a boolean
• list() - converts to a list
• tuple() - converts to a tuple
• dict() - converts to a dictionary
• set()- converts to a set
# Converting to int
string_number = "42“
integer_number = int(string_number)
print("Converted to int:", integer_number)
Output:
Converted to int: 42
# Converting to float
integer_value = 25
float_value = float(integer_value)
print("Converted to float:", float_value)
# Converting to list
tuple_values = (1, 2, 3)
list_values = list(tuple_values)
print("Converted to list:", list_values)
Coding Questions
1. Write a Python program to demonstrate string concatenation and repetition.
Create a simple code snippet that shows how to concatenate and repeat strings in Python.
Uber Ola Swiggy Zomato
2. Implement a function that takes a list and returns a tuple containing the first and last elements.
Write a function that accepts a list and returns a tuple with the first and last elements of the list.
Paytm PhonePe Razorpay
1. Create a simple program that defines variables of different data
types (int, float, str) and prints their types using the type()
function.
2. Write a function that takes a numeric value and returns its string
and boolean representations using str() and bool().
3. Implement a small program that creates a list of mixed data types
(int, str, float) and demonstrates indexing and slicing.
Solution 1
var1 = 10
var2 = 3.14
var3 = "Hello"
print(type(var1), type(var2), type(var3))
Solution 2
def convert_value(num):
return str(num), bool(num)
Solution 3
# Accepting.numerical input
age = int(input("Enter your age: "))
print("You are", age, "years old.")
1. Create a simple program that prompts the user for their favorite
color and then prints a message that includes their response.
2. Write a program that asks the user for two numbers, then prints
their sum.
3. Build a program that asks for the user's name and age, then prints
a personalized greeting that includes both pieces of information.
1.
color = input("What is your favorite color? ")
print("Your favorite color is", color)
2.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print("The sum is", num1 + num2)
3.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}, you are {age} years old!")
Arithmetic Operators
a = 10
b=3
print("Addition:", addition)
addition = a + b print("Subtraction:", subtraction)
subtraction = a - b print("Multiplication:", multiplication)
multiplication = a * b print("Division:", division)
division = a / b print("Floor Division:", floor_division)
print("Modulus:", modulus)
floor_division = a // b print("Exponentiation:", exponentiation)
modulus = a % b
exponentiation = a ** b
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponentiation: 1000
Comparison Operators:
x = 5, y = 7
# Equal to (==) operator
print(f"x == y : {x == y}")
x == y : False
x != y : True
x > y : False
x < y : True
x >= y : False
x <= y : True
a = True, b = False
logical_and = a and b
logical_or = a or b
logical_not_a = not a
logical_not_b = not b
print(f'a and b : {logical_and}')
print(f'a or b : {logical_or}')
print(f'not a : {logical_not_a}')
print(f'not b : {logical_not_b}')
Output:
a and b : False
a or b : True
not a : False
not b : True