Python Basic
What is Python?
Python is a popular programming language. It was created by
Guido van Rossum, and released in 1991.
Why Python?
Python works on different platforms (Windows, Mac, Linux etc).
Python has a simple syntax similar to the English language.
Introduction Python runs on an interpreter system, meaning that code can
be executed as soon as it is written. This means that prototyping
can be very quick.
Python can be treated in
procedural way,
object-oriented
functional way.
Latest Version Python 3.13.7
Version
Popular • PyCharm
Python IDEs • Visual Studio
Code (VS Code)
• Spyder
• Jupyter Notebook
Before we dive into the set-up procedure, let's short
talk about why PyCharm
Feature-wealthy Environment: PyCharm offers a
comprehensive set of capabilities along with debugging,
version manipulation integration, and more.
Smart Code Assistance: It provides wise code final
PyCharm touch and guidelines, which could appreciably speed up
your coding method and reduce mistakes.
Cross-Platform Support: PyCharm is to be had for
Windows, macOS, and Linux
Community and Professional Versions: You can pick
between the unfastened Community version and the
extra characteristic-rich Professional version, depending
on your needs and budget.
Variables are containers for storing data values
A variable is created the moment you first assign a value to it.
Example#1
x = 10
y = “Hello"
Python Variable print(x)
print(y)
s In Python, variables do not have fixed types. They are
dynamically typed, meaning the type can change when you
assign a new value.
Example#2
x=4 # x is of type int
x = “10A" # x is now of type str
print(x)
#Multiple Variables
#Global Variables x, y, z = "Orange", "Banana", "Cherry"
print(x)
x = "awesome" print(y)
print(z)
def myfunc():
print("Python is " +
x) #One Value to Multiple Variables
x = y = z = "Orange"
myfunc()
print(x)
print(y)
print(z)
x, y, z = 'Hello World'
x = y = z = 'Hello World'
x|y|z = 'Hello World'
def myfunc(): What is a correct syntax to add
global x the value 'Hello World', to 3
x = "fantastic" variables in one statement?
myfunc()
x, y, z = 'Hello World'
What will be the print("Python is " + x = y = z = 'Hello World'
printed result? x) x|y|z = 'Hello World'
x = 'awesome'
def myfunc():
x = 'fantastic' x, y, z = 'Hello World'
x = y = z = 'Hello World'
x|y|z = 'Hello World'
myfunc()
print('Python is ' + x)
When working with Python, knowing which data structure
to use is key to writing efficient code. But with so many
options
Lists
Tuples,
Sets,
Dictionary
it can be confusing to know which one to choose!
•Definition: An ordered collection of
items that is mutable (changeable).
•Syntax: my_list = [1, 2, 3, 4]
•Ordered: Elements have a specific
order and can be accessed by
index.
List •Mutable: You can add, remove, or
change elements.
•Allows Duplicates: The same
value can appear multiple times.
•When to Use: Use lists when you need an
ordered, changeable collection of items
# Creating a list
fruits = ['apple', 'banana', 'cherry', 'banana']
# Adding an element
[Link]('orange') # Adding 'orange' to the list
# Removing an element
[Link]('banana') # Removing one occurrence of
List Example 'banana'
# Accessing elements
first_fruit = fruits[0] # Accessing the first element
# Displaying the list
print("List of fruits:", fruits) # Output: List of fruits: ['apple',
'cherry', 'banana', 'orange']
print("First fruit:", first_fruit) # Output: First fruit: apple
Definition: An ordered collection of items that is
immutable (unchangeable).
Syntax: my_tuple = (1, 2, 3, 4)
Ordered: Like lists, tuples maintain a specific order.
Immutable: Once created, you cannot change the elements
Tuple (no adding, removing, or altering).
Allows Duplicates: Similar to lists, tuples can contain
duplicate values.
When to Use: Use tuples when you want to store a fixed
collection of items that shouldn’t change.
# Creating a tuple
vegetables = ('carrot', 'potato', 'onion')
# Accessing elements
first_vegetable = vegetables[0] # Accessing the first element
Example
# Displaying the tuple
print("Tuple of vegetables:", vegetables) # Output: Tuple of
vegetables: ('carrot', 'potato', 'onion')
print("First vegetable:", first_vegetable) # Output: First
vegetable: carrot
•Definition: An unordered collection of unique
items.
•Syntax: my_set = {1, 2, 3, 4}
•When to Use: Use sets when you need a collection of unique
items, such as to eliminate duplicates.
Set
# Creating a set
colors = {'red', 'green', 'blue', 'red'} # 'red' will be stored only
once
# Adding an element
[Link]('yellow') # Adding 'yellow' to the set
Example # Removing an element
[Link]('green') # Removing 'green' from the set
# Displaying the set
print("Set of colors:", colors) # Output: Set of colors: {'blue',
'red', 'yellow'
•Definition: An unordered collection of key-
value pairs.
•Syntax: my_dict = {'key1': 'value1', 'key2':
'value2’}
Unordered: The order of items is not guaranteed (but is
ordered as of Python 3.7).
Dictionary Mutable: You can change the values or add new key-value
pairs.
Unique Keys: Each key must be unique; values can be
duplicated.
When to Use: Use dictionaries when you need to associate
values with unique keys for quick lookup
# Creating a dictionary
person = {
'name': 'Alice',
'age': 30,
'city': 'New York'
}
# Adding a new key-value pair
person['job'] = 'Engineer' # Adding 'job' to the dictionary
Example # Accessing a value
person_age = person['age'] # Accessing the value of 'age'
# Removing a key-value pair
del person['city'] # Removing 'city' from the dictionary
# Displaying the dictionary
print("Person dictionary:", person) # Output: Person
dictionary: {'name': 'Alice', 'age': 30, 'job': 'Engineer'}
print("Person's age:", person_age) # Output: Person's age: 30