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

Python Basic Syntax

The document provides an overview of basic programming concepts in Python, including printing output, variables, user input, conditional statements, loops, functions, nested functions, lists, dictionaries, file handling, and exception handling. Each section includes code examples and expected outputs to illustrate the concepts. This serves as a foundational guide for beginners learning Python programming.

Uploaded by

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

Python Basic Syntax

The document provides an overview of basic programming concepts in Python, including printing output, variables, user input, conditional statements, loops, functions, nested functions, lists, dictionaries, file handling, and exception handling. Each section includes code examples and expected outputs to illustrate the concepts. This serves as a foundational guide for beginners learning Python programming.

Uploaded by

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

1.

Printing Output
(Prints text to the console)
# Print "Hello, World!" to the screen

print("Hello, World!")

Output:
Hello, World!

2. Variables and Data Types


(Variables store values of different data types)
# Assigning different data types to variables

name = "Alice" # String

age = 25 # Integer

height = 5.6 # Float

is_student = True # Boolean

# Printing variables

print(name, age, height, is_student)

Output:
Alice 25 5.6 True

3. Taking User Input


(Accepts input from the user and prints it)
# Asking for user input

name = input("Enter your name: ")


# Printing user input

print("Hello, " + name)

Example Input:
Enter your name: Bob

Output:
Hello, Bob

4. Conditional Statements (if-elif-else)


(Used to make decisions in the code)
# Checking if a number is positive, negative, or zero

num = -5

if num > 0:

print("Positive")

elif num < 0:

print("Negative")

else:

print("Zero")

Output:
Negative

5. Loops (for and while)


(Loops help in executing code multiple times)
# For loop - Iterates a fixed number of times
for i in range(3): # Loops from 0 to 2

print(i)

# While loop - Runs as long as the condition is True

count = 0

while count < 3:

print(count)

count += 1

Output:
0

6. Functions
(Reusable blocks of code that perform a specific task)
# Defining a function that takes a name and returns a greeting

def greet(name):

return "Hello, " + name

# Calling the function and printing the result

print(greet("Alice"))

Output:
Hello, Alice

7. Nested Functions
(A function inside another function)
# Function with a nested function

def outer_function(name):

print("Outer function running...")

def inner_function():

print("Inner function says: Hello, " + name)

inner_function() # Calling the inner function

# Calling the outer function

outer_function("Bob")

Output:
Outer function running...

Inner function says: Hello, Bob

8. Lists (Arrays)
(Used to store multiple values in a single variable)
# Creating a list of fruits

fruits = ["Apple", "Banana", "Cherry"]

# Accessing elements from the list (index starts from 0)

print(fruits[1]) # Output: Banana


# Adding an element to the list

fruits.append("Orange")

print(fruits)

Output:
Banana

['Apple', 'Banana', 'Cherry', 'Orange']

9. Dictionaries (Key-Value Pairs)


(Used to store data in key-value format)
# Creating a dictionary with key-value pairs

person = {"name": "Alice", "age": 25}

# Accessing values using keys

print(person["name"])

print(person["age"])

# Adding a new key-value pair

person["city"] = "New York"

print(person)

Output:
Alice

25

{'name': 'Alice', 'age': 25, 'city': 'New York'}


10. File Handling (Read and Write to Files)
(Read from and write to a file)

Writing to a File
# Open a file in write mode and add text

with open("example.txt", "w") as file:

file.write("Hello, this is a file!\n")

file.write("Python makes file handling easy.")

Reading from a File


# Open the file in read mode and display its contents

with open("example.txt", "r") as file:

content = file.read()

print(content)

Output (example.txt file content):


Hello, this is a file!

Python makes file handling easy.

11. Exception Handling (try-except)


(Handles runtime errors to prevent program crashes)
try:

# Attempting to divide by zero (which causes an error)

x = 10 / 0

except ZeroDivisionError:

# Handling the division by zero error

print("Cannot divide by zero!")

Output:
Cannot divide by zero!

You might also like