Hello friends! Today, we are diving into the core of Python programming by exploring calculations, variables, data types, and input/output. As hackers, understanding these Python fundamentals is essential for writing scripts, automating tasks, and developing tools. Let’s get started!
Python as a Calculator
You can easily use Python as a calculator to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. No need for complex syntax, just open your terminal (Linux users) or CMD (Windows users), or if you're on an Android device, you can use Termux.
Here’s a quick overview of the basic calculations in Python:
1. Addition:
print(10 + 5)
Output : 15
2. Subtraction:
print(10 - 5)
Output : 5
3. Multiplication:
print(10 * 5)
Output : 50
4. Division:
print(10 / 5)
Output : 2.0
You can perform these operations directly in the Python interpreter or write them in a script and run it. Python handles arithmetic just like a regular calculator, which is why it’s so handy for quick calculations.
Variables in Python
In Python, variables act as containers for storing data. You can think of variables like containers in your kitchen that store food – they help us store different types of data like numbers, names, or decimal values. Let’s break this down:
Rules for Variables:
- No special characters: Avoid using special characters or numbers at the beginning of variable names.
- Avoid reserved words: Don’t use Python keywords (like
print
,if
,for
) as variable names.
Here are the three main types of variables:
1. Strings: Strings store characters or words, which are always enclosed in quotes. For example:
name = "Vaibhav" print(name)
2. Integers: Integers store whole numbers (no decimal points). For example:
number = 100 print(number)
3. Floating-Point Numbers: These store numbers with decimal points. For example:
value = 22.55 print(value)
Example:
# String string_var = "This is a string" print(string_var) # Integer int_var = 150 print(int_var) # Float float_var = 10.45 print(float_var)
Data Types in Python
Python provides various data types to store different types of values. Each data type has its own purpose, allowing us to manage and manipulate data in different ways. Let’s take a look at the main Python data types:
1. Strings (str
):
- Ordered sequences of characters like "hello", "world".
- Example:
"Vaibhav"
,"Hacker"
2. Integers (int
):
- Whole numbers without decimal points.
- Example:
100
,75
3. Floating-point (float
):
- Numbers with decimal points.
- Example:
10.5
,524.44
4. Lists (list
):
- Ordered sequences that can hold different data types: strings, integers, or floats.
- Example:
[1, "Python", 3.5]
5. Dictionaries (dict
):
- Unordered collections of key-value pairs.
- Example:
{"name": "Vaibhav", "role": "Hacker"}
6. Tuples (tuple
):
- Immutable sequences that cannot be changed once defined.
- Example:
(10, "Python", 3.5)
7. Sets (set
):
- Unordered collections of unique elements.
- Example:
{"apple", "banana", "cherry"}
8. Booleans (bool
):
- Logical values representing
True
orFalse
. - Example:
True
,False
# Data Types in action string_var = "Hacker" int_var = 10 float_var = 10.5 list_var = [1, 2, 3, "hello"] dict_var = {"name": "Vaibhav", "age": 25} tuple_var = (5, "world") set_var = {1, 2, 3, 4} print(type(string_var)) # class 'str' print(type(int_var)) # class 'int' print(type(float_var)) # class 'float'
Strings in Python:
String Basics
Strings in Python are used to store text and are enclosed within either single ('
) or double ("
) quotes. For example:
my_string = "Hello, Python!"
You can also include quotes within a string:
quote = "Don't give up!"
String Indexing:
Each character in a string has an index starting from 0. For example:
word = "hello" print(word[0]) # Output: h print(word[4]) # Output: o
You can even use negative indexing to count characters from the end:
my_string = "Hello, Python!"
String Slicing:
You can extract portions of a string using slicing. For example:
print(word[1:4]) # Output: ell
String Methods:
Here are some commonly used string methods:
print(word[1:4]) # Output: ell
1. len(): Measures the length of a string.
print(len("hello")) # Output: 5
2. upper(): Converts a string to uppercase.
print("hello".upper()) # Output: HELLO
3. lower():Converts a string to lowercase.
print("HELLO".lower()) # Output: hello
4. replace():Replaces characters in a string.
print("hello".replace('e', 'a')) # Output: hallo
5. index():Finds the index of a character.
print("hello".index('e')) # Output: 1
Lists in Python
Lists are a collection of ordered objects, and they allow you to store multiple items in a single variable. Lists are created using square brackets []
.
List Example:
my_list = [1, 2, "Python", 4.5] print(my_list) # Output: [1, 2, 'Python', 4.5]
List Methods
Here are some common list methods:
1. append()
: Adds an item to the end of the list.
my_list.append(10)
2. remove()
: Removes the first occurrence of an item.
my_list.remove(2)
3. pop()
: Removes an item from a specific index.
my_list.pop(1)
4. reverse()
: Reverses the order of the list.
my_list.reverse()
Dictionaries in Python
Dictionaries hold key-value pairs and are declared using curly braces {}
. Each item in a dictionary is a pair of a key and its value.
Dictionary Example:
person = {"name": "Vaibhav", "role": "Hacker"} print(person["name"]) # Output: Vaibhav
Dictionary Methods
keys()
: Returns a list of keys.values()
: Returns a list of values.items()
: Returns key-value pairs.
Conclusion
These are the basics of Python’s data types, variables, and input/output operations that will form the foundation for writing more advanced Python programs. As hackers, mastering these concepts will help you create tools, automate tasks, and develop scripts with ease.
If you found this post helpful, share it with your friends and continue learning Python! Stay tuned for more hacking tips and Python tricks in the next blog.
Best of luck!
Medium Blog: Python for Hackers
Comments
Post a Comment