0% found this document useful (0 votes)
8 views12 pages

Python 1

The document provides an overview of Python programming, covering its introduction, history, installation, and basic programming concepts such as syntax, data types, control structures, functions, and data structures. It includes examples of code snippets and explanations of key concepts like variables, loops, and functions. Additionally, it features questions to assess understanding of the material presented in the chapters.

Uploaded by

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

Python 1

The document provides an overview of Python programming, covering its introduction, history, installation, and basic programming concepts such as syntax, data types, control structures, functions, and data structures. It includes examples of code snippets and explanations of key concepts like variables, loops, and functions. Additionally, it features questions to assess understanding of the material presented in the chapters.

Uploaded by

yasmeen23fathima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SYSTEM TRON INTERNSHIP NOTES

Python PART 1

Chapter 1: Introduction to Python


1.1 What is Python?

N
1.2 Python's History and Evolution
1.3 Python Installation
1.4 Writing Your First Python Program

O
Chapter 1: Introduction to Python

TR
In this chapter, we'll start with the basics of Python, a popular programming language. We'll
cover what Python is, its history, how to install it, and how to write your very first Python
program.

1.1 What is Python?


EM
Python is a computer programming language. It's like a tool that allows you to give instructions
to a computer. You can use Python to make the computer do all sorts of tasks, from simple
calculations to creating complex applications.

1.2 Python's History and Evolution

Python was created by Guido van Rossum and first released in 1991. It has since evolved and
grown into a powerful and easy-to-read language. Python is known for its simplicity and
ST

readability, making it a great choice for beginners and experts alike.

1.3 Python Installation

Before you can use Python, you need to install it on your computer. Here are the basic steps:

- Go to the Python website (https://www.python.org/).


SY

- Download the installer for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the on-screen instructions.

1.4 Writing Your First Python Program

Let's write a simple Python program that displays a message. Open a text editor (like Notepad
on Windows or TextEdit on macOS) and type the following code:
Python Code
print("Hello, Python!")

Save the file with a ".py" extension, for example, "first_program.py."

N
Now, open your computer's command prompt or terminal, navigate to the folder where you
saved your program, and type:

O
python first_program.py

You should see the message "Hello, Python!" printed on the screen. Congratulations, you've just

TR
run your first Python program!

That's it for the first chapter. You've learned what Python is, how it has evolved, how to install it,
and you've even written a simple program. As you continue your journey, you'll discover all the
amazing things you can do with Python.
EM
Chapter 2: Basic Syntax and Data Types
2.1 Indentation and Code Blocks
2.2 Variables and Data Types (int, float, str, bool)
2.3 Type Conversion
ST

2.4 Comments and Docstrings

Chapter 2: Basic Syntax and Data Types

2.1 Indentation and Code Blocks


SY

In Python, we don't use curly braces like some other programming languages to define code
blocks. Instead, we use indentation. Indentation means the spaces or tabs at the beginning of a
line. It helps Python understand which lines of code belong together.

For example, in a loop or a function, you need to make sure that all the lines inside it are
indented by the same amount. Here's an example of a simple loop:
Python Code
for i in range(5):
print("This line is indented.")
print("So is this one.")
print("This line is not indented and is outside the loop.")

N
2.2 Variables and Data Types (int, float, str, bool)

O
Variables are like containers where you can store data. Python has different data types to
represent different kinds of information.

- int : Used for whole numbers (integers). Example: `age = 25`

TR
- float : Used for numbers with decimals. Example: `price = 19.99`

- str : Used for text (strings). Example: `name = "Alice"`

- bool : Used for true or false values. Example: `is_happy = True`


EM
You can change the value of a variable whenever you want.

2.3 Type Conversion

Sometimes, you need to convert one data type into another. Python allows you to do this. For
example, you can convert an integer to a string like this:

Python Code
number = 42
ST

text = str(number) # Now 'text' contains the string "42"

2.4 Comments and Docstrings

Comments are notes you can write in your code to explain what it does. Python ignores
comments when running your program. You can write a comment using the `#` symbol.
SY

Python Code
# This is a comment

Docstrings are comments that provide documentation about a function, class, or module. They
are enclosed in triple quotes. They are used to explain how to use a piece of code.
Python Code
def greet(name):
"""
This function greets the person passed in as a parameter.
"""

N
print("Hello, " + name)

In summary, in this chapter, you learned about the basics of Python's syntax, how to use

O
variables for different data types, how to convert between data types, and how to add comments
and docstrings to your code for better understanding and documentation.

TR
Chapter 3: Control Structures
3.1 Conditional Statements (if, elif, else)
3.2 Loops (for, while)
EM
3.3 Break and Continue Statements
3.4 Iterators and Generators

Chapter 3: Control Structures


ST

3.1 Conditional Statements (if, elif, else):

Conditional statements help your program make decisions. You can think of them like making
choices in your daily life. Here's how they work in Python:

- if statement: If a condition is true, do something. If it's not true, don't do anything.


SY

Python Code
age = 18
if age >= 18:
print("You can vote!")
- elif statement: If the first condition is not true, check another condition.

Python Code
temperature = 25
if temperature > 30:
print("It's hot outside.")

N
elif temperature > 20:
print("It's a pleasant day.")

O
- else statement: If none of the conditions before it are true, do something else.

Python Code
grade = 60

TR
if grade >= 70:
print("You passed!")
else:
print("You didn't pass.")

3.2 Loops (for, while):


EM
Loops help you repeat tasks. Imagine doing something over and over like counting. In Python,
there are two types of loops:

- for loop: You can use it to go through a list of items or repeat an action a specific number of
times.

Python Code
for number in range(1, 6):
ST

print(number)

This will print numbers 1 to 5.

- while loop: Use it when you want to repeat something until a condition is no longer true.
SY

Python Code
count = 1
while count <= 5:
print("Hello!")
count += 1
This will print "Hello!" five times.

3.3 Break and Continue Statements:

These statements help control the flow of your loops.

N
- break: It allows you to exit a loop early if a certain condition is met.

Python Code
numbers = [1, 2, 3, 4, 5]

O
for number in numbers:
if number == 3:
break
print(number)

TR
This will print 1 and 2 and then stop.

- continue: It allows you to skip the rest of the loop and move to the next iteration.

Python Code
EM
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
continue
print(number)

This will print all numbers except 3.


ST

3.4 Iterators and Generators:

These are like special loops and make working with large sets of data easier.

- Iterators: You can use them to go through items one at a time without loading everything into
memory.
SY

Python Code
my_list = [1, 2, 3]
my_iterator = iter(my_list)
print(next(my_iterator))

This will print 1 and then you can continue using `next()` to get the next item.
- Generators: They're like iterators but easier to create and read. They generate values one at
a time when you need them.

Python Code
def my_generator():

N
yield 1
yield 2
yield 3

O
gen = my_generator()
print(next(gen))

TR
This will also print 1 and then you can use `next()` to get the next values.

That's the basic idea of Control Structures in Python. They help you make decisions, repeat
tasks, and control the flow of your code.
EM
Chapter 4: Functions
4.1 Function Definition and Syntax
4.2 Function Parameters
4.3 Return Statements
4.4 Scope and Lifetime of Variables
4.5 Lambda Functions
ST

Chapter 4: Functions
SY

4.1 Function Definition and Syntax

In Python, a function is like a mini-program that you can create and use whenever you want. It's
a way to group a bunch of code together to perform a specific task.

Example of defining a simple function:

Python Code
def greet():
print("Hello, world!")

# To use the function, you call it like this:


greet()

N
4.2 Function Parameters

Functions can take input values called parameters or arguments. These values allow your

O
function to be more flexible and do different things based on what you provide.

Example of a function with parameters:

TR
Python Code
def greet(name):
print(f"Hello, {name}!")

greet("Alice")
greet("Bob")
EM
4.3 Return Statements

Functions can also give you back a result or answer using the `return` statement. This result can
be used in your program.

Example of a function with a return statement:

Python Code
ST

def add(a, b):


result = a + b
return result

sum = add(3, 4)
print("The sum is:", sum)
SY

4.4 Scope and Lifetime of Variables

Variables inside functions have a scope, which means they only exist and can be used within
that function. Once the function finishes, the variables disappear.

Example of variable scope:


Python Code
def my_function():
x = 10
print(x)

N
my_function()
# You can't access x here because it's inside the function.

O
4.5 Lambda Functions

Lambda functions are small, anonymous functions. They can have any number of arguments
but can only have one expression. They are often used when you need a simple function for a

TR
short period.

Example of a lambda function:

Python Code
# Define a lambda function to double a number
double = lambda x: x * 2
EM
result = double(5)
print("Double of 5 is", result)

In summary, functions are like custom tools that you can create to make your code more
organized and reusable. You can give them inputs (parameters) and get results (return values).
Understanding how variables work inside functions and using lambda functions can make your
code more powerful and efficient.
ST

Chapter 5: Data Structures


5.1 Lists
SY

5.2 Tuples
5.3 Dictionaries
5.4 Sets
5.5 List Comprehensions
Chapter 5: Data Structures

Data structures in Python are like containers that help you store and organize your data. They
are like different types of boxes to hold things. In this chapter, we'll explore five important data
structures: Lists, Tuples, Dictionaries, Sets, and List Comprehensions.

N
5.1 Lists

- What is a List? A list is like a shopping list where you can write down multiple items in order.

O
In Python, a list is a collection of items enclosed in square brackets `[ ]`.

- Example: Here's a list of fruits:

TR
Python Code
fruits = ["apple", "banana", "cherry"]

5.2 Tuples

- What is a Tuple? A tuple is similar to a list, but once you create it, you can't change its
EM
contents. It's like a sealed bag of items. In Python, a tuple is enclosed in parentheses `( )`.

- Example: A tuple of coordinates:

Python Code
coordinates = (3, 4)

5.3 Dictionaries
ST

- What is a Dictionary? A dictionary is like a real dictionary with words and their meanings. In
Python, a dictionary is a collection of key-value pairs enclosed in curly braces `{ }`.

- Example: A dictionary of a person's information:

Python Code
SY

person = {"name": "Alice", "age": 30, "city": "New York"}

5.4 Sets

- What is a Set? A set is like a collection of unique items. You can't have duplicates. In Python,
a set is enclosed in curly braces `{ }` or created using the `set()` function.
- Example: A set of colors:

Python Code
colors = {"red", "green", "blue"}

N
5.5 List Comprehensions

- What is List Comprehension? List comprehension is a fancy way to create lists. It's like a

O
shortcut to make lists in just one line instead of using a loop.

- Example: Creating a list of even numbers from 1 to 10 using list comprehension:

TR
Python Code
even_numbers = [x for x in range(1, 11) if x % 2 == 0]

So, these data structures help you work with data more efficiently. Lists and tuples are for
ordered collections, dictionaries are for key-value pairs, sets are for unique items, and list
comprehensions are for creating lists quickly. You can use these structures to solve different
EM
types of problems in Python.

Questions 👍
ST

Chapter 1: Introduction to Python

1. What is Python, and why is it a popular programming language?


2. Briefly explain the history and evolution of Python.
3. What are the basic steps involved in writing and running a Python program?
4. Provide an example of a simple Python program, and explain its components.
5. What are some common applications of Python programming?
SY

Chapter 2: Basic Syntax and Data Types

1. What is indentation in Python, and why is it important?


2. Name and briefly describe four basic data types in Python.
3. How can you convert one data type to another in Python?
4. Explain the purpose of comments and docstrings in Python code.
5. Write a Python code snippet that calculates the area of a rectangle given its length and width.

Chapter 3: Control Structures

1. Describe the use of conditional statements in Python with examples.


2. What is a loop, and why is it used in programming? Provide examples of "for" and "while"

N
loops.
3. How can you control the flow of a loop using "break" and "continue" statements?
4. Explain the concept of iterators and provide a use case for iterators in Python.
5. Write a Python program that counts the number of even numbers in a list.

O
Chapter 4: Functions

1. Define a function in Python and explain its components.

TR
2. How do you pass parameters to a function, and what are the different ways to return values
from a function?
3. Describe the concept of variable scope in Python functions.
4. What is a lambda function, and how is it different from a regular function?
5. Write a Python function that calculates the factorial of a number.

Chapter 5: Data Structures


EM
1. Compare and contrast lists and tuples in Python. When would you use one over the other?
2. Explain the key characteristics of dictionaries and provide an example of using a dictionary in
Python.
3. What is a set in Python, and how is it different from a list or tuple?
4. Describe the purpose of list comprehensions and provide an example of using list
comprehensions.
5. Create a Python program that uses a dictionary to store contact information (name, email,
phone) for multiple contacts.
ST

These questions should help you assess your knowledge and comprehension of the topics
covered in the first six chapters of Python and OOP concepts.
SY

You might also like