PYTHON PROGRAMMING.pptx (1)
PYTHON PROGRAMMING.pptx (1)
What is Python?
Python"
Python is a high-level, interpreted programming language known for its
simplicity and readability.
Python has a simple syntax similar to English, making it easy to read and
write.
1. Download Python:
Go to the official Python website.
Download the latest version of Python (look
for the Windows installer).
2. Run the Installer:
Open the downloaded installer.
Check the box that says “Add Python to
PATH.”
Click on “Install Now.”
Python Modes
Python has two main modes:
1. script mode
2. interactive mode:
Interactive mode
In this mode, you type commands and they are
immediately executed. It's best for testing and
experimenting with Python, and for quick
debugging. Interactive mode is also useful for
exploratory programming and prototyping.
However, it's not recommended for writing long
pieces of code or scripts that span multiple
files.
Script mode
In this mode, you put a series of
commands into a file, and then tell
Python to run the file. It's best for
developing and running more
substantial Python programs, and
for automating tasks and creating
standalone applications. Script
mode also allows you to save your
code in a file for future use.
Syntax Of Python
Python syntax refers to the set of rules that defines how Python code is written and
structured. Here are some key elements of Python syntax:
Variables
Variables are used to store data values.
Control Flow
Python includes several control flow statements:
if statements:
z=3
if z % 2 == 0:
print("z is divisible by 2")
elif z % 3 == 0:
print("z is divisible by 3")
else:
print("z is neither divisible by 2 nor by 3")
for loops:
for i in range(5):
print(i)
while loops:
With the while loop we can execute a set of statements as long as a
condition is true.
i=1
while i < 6:
print(i)
i=i+1
PYTHON TOKENS
"Tokens" refer to the smallest units of code that the Python interpreter
recognizes. These include keywords, identifiers, literals, operators, and
delimiters. Here’s a quick overview:
== : Equal to
!= : Not equal to
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to
3. Logical
Operators
These operators are used to
combine conditional statements.
4.
PYTHON DATA TYPES:
There are five major data types in python:
1. List
2. String
3. Tuple
4. Numbers
5. Dictionary
STRINGS
IN PYTHON
In Python, a string is a sequence of characters
used to represent text. Strings can include
letters, numbers, symbols, and whitespace. They
are one of the fundamental data types in Python
and are defined by enclosing characters in either
single quotes ('), double quotes ("), triple single
quotes ('''), or triple double quotes (""").
greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(message) # Output: Hello, Alice!
LISTS IN PYTHON
In Python, a list is a built-in data structure that
allows you to store an ordered collection of
items. Lists can hold a variety of data types,
including integers, floats, strings, and even other
lists. They are mutable, meaning you can change
their contents without creating a new list.
For example:
fruits.append("mango")
fruits.insert(1, "blueberry") # Inserts
'blueberry' at index 1
TUPLES IN PYTHON
In Python, a tuple is a built-in data
structure that is similar to a list, but
with a key difference: tuples are
immutable. This means that once a
tuple is created, its contents cannot be
changed. Tuples are typically used to
group related data together.
For example:
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # Output: 'apple'
print(fruits[1]) # Output: 'banana'
Difference between
Lists and Tuples
DICTIONARY IN PYTHON
In Python, a dictionary is a built-in data
structure that stores data in key-value pairs.
Dictionaries are unordered, mutable, and
indexed by keys, which can be of any
immutable type (like strings, numbers, or
tuples). They are particularly useful for
scenarios where you want to associate values
with unique keys.
For example:
my_dict = { "name": "Alice", "age": 30, "city": "New York" }
another_dict = dict(name="Bob", age=25, city="Los Angeles")
print(my_dict["name"]) # Output: Alice
print(my_dict["age"]) # Output: 30
FUNCTIONS
In Python, a function is a reusable block of code that performs a specific task. Functions help organize
code, make it more readable, and avoid repetition. They can take inputs (arguments) and return outputs.
Th
DATA FILE HANDLING
File handling in Python allows you to read from and write to files, which is essential
for data persistence and manipulation. Python provides built-in functions to work
with files easily. Here’s a comprehensive overview of file handling in Python.
File Modes
Here are some common file modes:
'r': Read (default mode). Opens a file for reading.
'w': Write. Opens a file for writing (creates a new
file or truncates an existing one).
'a': Append. Opens a file for appending (writes
data at the end of the file).
'b': Binary mode. Can be added to any mode to
handle binary files (e.g., 'rb', 'wb').
TEXT FILES
Python involves various operations, such as
creating, reading, writing, and appending
data. Here's a detailed overview of how to
handle text files in Python, including
examples.
BINARY FILES
In Python, binary files are files that contain
data in a format that is not plain text. These
files can include images, audio, video, and other
types of data that require specific encoding.
When working with binary files, it's essential to
use binary modes for reading and writing.
STACK
Stack means arranging one object over another
and this object are remove in reverse order of
their arrival .The stack is called Last In First
Out(LIFO) arrangement. You can implement a
stack using lists.
For example:
stack = [1, 2, 3]
top_item = stack.pop()
print("Popped item:", top_item)
.
THANK YOU