0% found this document useful (0 votes)
2 views5 pages

Basics Python

The document provides a comprehensive introduction to Python programming, covering topics such as variables, data types, input handling, conditional statements, loops, lists, functions, string operations, dictionaries, and list comprehensions. It includes code examples and outputs for each concept, demonstrating their usage in practical scenarios. Additionally, it presents a problem-solving example to reinforce the concepts learned.

Uploaded by

Syed Shuaib
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Basics Python

The document provides a comprehensive introduction to Python programming, covering topics such as variables, data types, input handling, conditional statements, loops, lists, functions, string operations, dictionaries, and list comprehensions. It includes code examples and outputs for each concept, demonstrating their usage in practical scenarios. Additionally, it presents a problem-solving example to reinforce the concepts learned.

Uploaded by

Syed Shuaib
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Step 1: Python Basics – Variables, Data Types, and

Printing
Variables store values in memory. Python doesn’t need explicit type declarations.
# Integer
age = 25

# Float
price = 19.99

# String
name = "Syed"

# Boolean
is_coding = True

print("Name:", name)
print("Age:", age)
print("Price:", price)
print("Is Coding?", is_coding)

Output:
Name: Syed
Age: 25
Price: 19.99
Is Coding? True

Step 2: Input and Type Casting


Amazon-style coding problems will often need you to read input from the user and
process it.
# Reading input as a string
name = input("Enter your name: ")

# Reading input as integer


age = int(input("Enter your age: "))

# Reading multiple inputs in one line


a, b = map(int, input("Enter two numbers: ").split())

print("Hello", name)
print("Sum of numbers:", a + b)

Example Input:
Syed
22
4 5
Output:
Hello Syed
Sum of numbers: 9

Step 3: Conditional Statements


We use if, elif, else to make decisions.
n = int(input("Enter a number: "))

if n > 0:
print("Positive number")
elif n < 0:
print("Negative number")
else:
print("Zero")

Example Input:
5

Output:
Positive number

Step 4: Loops (for & while)


Used to repeat code blocks.
# For loop (range)
for i in range(1, 6):
print("Number:", i)

# While loop
count = 1
while count <= 5:
print("Count:", count)
count += 1

Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Step 5: Lists and Their Operations


Lists store multiple values and are mutable.
numbers = [10, 20, 30, 40]

# Access elements
print(numbers[0]) # First element

# Modify element
numbers[1] = 25

# Append element
[Link](50)

# Iterate
for num in numbers:
print(num)

Output:
10
25
30
40
50

Step 6: Functions
Functions help organize reusable code.
def add(a, b):
return a + b

result = add(5, 7)
print("Sum is:", result)

Output:
Sum is: 12

Step 7: String Operations


String manipulation is very common in Amazon problems.
text = "amazon"

print([Link]()) # AMAZON
print(text[::-1]) # nozamA (reverse)
print([Link]("a")) # 2
print([Link]("a", "@")) # @m@zon

Step 8: Dictionary Basics


Dictionaries store key-value pairs.
student = {"name": "Syed", "age": 22, "skills": ["Python", "DSA"]}

print(student["name"]) # Access value


student["age"] = 23 # Modify
student["city"] = "Bangalore" # Add new key-value

for key, value in [Link]():


print(key, ":", value)

Output:
name : Syed
age : 23
skills : ['Python', 'DSA']
city : Bangalore

Step 9: List Comprehensions


A shorter way to create lists.
squares = [x**2 for x in range(1, 6)]
print(squares)

Output:
[1, 4, 9, 16, 25]

Step 10: Problem-Solving Warm-Up Example


Let’s solve a simple Amazon-style problem.

Problem:
Given a list of numbers, find the sum of all even numbers.
n = int(input("Enter number of elements: "))
nums = list(map(int, input("Enter numbers: ").split()))
even_sum = sum([x for x in nums if x % 2 == 0])

print("Sum of even numbers:", even_sum)

Example Input:

5
1 2 3 4 5

Output:
Sum of even numbers: 6

You might also like