Skip to main content

Python Basics - Part I

*TL:DR*
Initial Installation of python in system 

 Lets start with simple python program


print("Hello World") #---> output ---> Hello world

"""
Hello world program 

In python anything inside print() will display on the screen

Also the text print to on the screen should be inside "", '' between this

"""


Python Commands



#print a number
print(25)

# Hash is used to comment out which will ignored on execution

# Hash is a single line command
"""
Multi line command Defined like this
can hold multiple lines
"""
'''
These commands can comment out error making code lines and also declare
 or define what this 
particular line of code does explanation

For debugging
For Future references, as comments are in readable format
For collaboration with Peers
'''


Python Variables and Literals


#variable is the container which holds or store data 
number = 10
#Number is the variable holds data value of 10

#Assign a value to a variable

site_name = "offensivestack" print(site_name) """ = is the assignment operator for a varaible """ #Changing the variable value of a python site_name = 'offensivestack' print(site_name) #assign a newvalue to site_name site_name = 'apple.com' print(site_name) """ initial value of site_name was offensive stack now it will be replaced with apple.com, cuz new value has been assigned """ #assigning multiple values to multiple variables a,b,c = 5,6.8,'Hi' print(a) # o/p ---> 5 print(b) # o/p --->6.8 print(c) # o/p ---> Hi #assign same value to multiple variables site1 = site2 = 'offensivestack.in' """ Rules for naming a variabe case sensitive (num & Num are different variable) under_variable - seperate by underscore """ #Literals is a fixed values in the variable #List literal fruits = ["apple", "Mango", "orange"] print(fruits) #tuple literals number = (1,2,3) print(number) # dictionary literal alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} print(alphabets) # set literal vowels = {'a', 'e', 'i' , 'o', 'u'} print(vowels)
Python Type Conversion



"""
Implicit type conversion - automatic type conversion
Explicit type conversion - Manual type conversion
"""

#convert Int to float
integer_number = 123
float_number = 1.23

new_number = integer_number + float_number

# display new value and resulting data type
print("Value:",new_number)
print("Data Type:",type(new_number))

"""
above code int value and float value get added and new value comes
the value automatically become a float value 
it is called implicit type conversion
"""


#Explicit type conversion - manual type conversion
num_string = '12'
num_integer = 23

print("Data type of num_string before Type Casting:",type(num_string))

# explicit type conversion
num_string = int(num_string) # manually change the data type to integer

print("Data type of num_string after Type Casting:",type(num_string))

num_sum = num_integer + num_string

print("Sum:",num_sum)
print("Data type of num_sum:",type(num_sum))

"""
Type Conversion is the conversion of an object from one data type to another data type.

Implicit Type Conversion is automatically performed by the Python interpreter.

Python avoids the loss of data in Implicit Type Conversion.

Explicit Type Conversion is also called Type Casting, the data types of objects
are converted using predefined functions by the user.

In Type Casting, loss of data may occur as we enforce the object to a specific data type.
"""



Python Basic Input and output

Print('python output')
# Basic output on display

#However, the actual syntax of the print function accepts 5 parameters
print(object= separator= end= file= flush=)

"""
object - value(s) to be printed

sep (optional) - allows us to separate multiple objects inside print().

end (optional) - allows us to add add specific values like new line "\n", tab "\t"

file (optional) - where the values are printed. It's default value is sys.stdout (screen)

flush (optional) - boolean specifying if the output is flushed or buffered. Default: False
"""

#Python print statement
print('Good')
print('day')
'''
Output be like
Good
Day
Print () statemement only includes the object to be printed
the value end is not used , it takes /n automatically and print two diff lines
'''

#Python print() with end parameter
# print with end whitespace
print('Good Morning!', end= ' ')

print('It is rainy today')

"""
Good Morning! It is rainy today
Notice that we have included the end= ' ' after the end of the first print() statement.

Hence, we get the output in a single line separated by space.
"""


#Python print() with sep parameter

print('New Year', 2023, 'See you soon!', sep= '. ')

"""
New Year. 2023. See you soon!
 
In the above example, the print() statement includes multiple items separated by a comma.

Notice that we have used the optional parameter sep= ". " inside the print() statement.

Hence, the output includes items separated by . not comma.

"""

#Print Python Variables and Literals
number = -10.6

name = "Programiz"

# print literals     
print(5)

# print variables
print(number)
print(name)


'''
5
-10.6
Programiz
'''

#Print Concatenated Strings
print('Programiz is ' + 'awesome.')

'''
Programiz is awesome.
Here,

the + operator joins two strings 'Programiz is ' and 'awesome.'
the print() function prints the joined string
'''


#Output formatting
x = 5
y = 10

print('The value of x is {} and y is {}'.format(x,y))

'''
Sometimes we would like to format our output to make it look attractive. 
This can be done by using the str.format() method.

Here, the curly braces {} are used as placeholders.
 We can specify the order in which they are printed by using numbers (tuple index).
'''



#PYTHON INPUT
#Input(prompt) is a function to take input from the user
num = input('Enter the number: ')
print(num)

"""
take a input from the user
and display on the screen
"""

Python Operators


'''
Arithemetic operators
Assignment Operators
Comparison Operators
Bitwise Operators
Special Operators
Logical Operators
'''

#Arithementic Operators

a = b + c #is an addition comes under artithemtic Operators

'''
Same Operations can be performed against +,-,/,*,%, **, 
'''

#Assignment Operators
a += 1 # ----> a = a + 1

'''
assignment operator value assigned to variable 
like a += b is a = a + b
same operations performed with addition,division and multiplication
'''

#Comparison Operators

a = 5
b = 2

print (a > b) # --> o/p -- true

'''
Comparsion operators compare two values and output the statement is true or false

==
>=
<=
!=

'''

#Logical operators

a = 5
b = 6

print((a > 2) and (b >= 6))    # True

'''
Logical operators check wheather the expression is true or false

AND
OR
Not
'''

#Bitwise Operators

'''
Bitwise operators act on operands as if they were strings of binary digits. 
They operate bit by bit,

& Bitwise AND
| Bitwise OR

'''

#Python Special Operators
'''
identity operator
Membership operator
'''

'''
Identity operators is and is not check two value located at the same memory location
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]

print(x1 is not y1)  # prints False

print(x2 is y2)  # prints True

print(x3 is y3)  # prints False

'''

#Membership Operators
'''
Membership operators used to test wheather a value or variable is found in a sequence 
message = 'Hello world'
dict1 = {1:'a', 2:'b'}

# check if 'H' is present in message string
print('H' in message)  # prints True

# check if 'hello' is present in message string
print('hello' not in message)  # prints True

# check if '1' key is present in dict1
print(1 in dict1)  # prints True

# check if 'a' key is present in dict1
print('a' in dict1)  # prints False

'''

Comments

Popular posts from this blog

Powershell Automation Basics - Part 1

Pentest Notes: PowerShell Automation - Basics Pentest Notes: PowerShell Automation - Basics These notes cover PowerShell automation for penetration testing, focusing on practical applications and techniques. What is PowerShell? A powerful scripting language and command-line shell built on the .NET framework, heavily integrated with Windows. Ideal for system administration and automation, making it a valuable tool for pentesters. Why PowerShell for Pentesting? Native to Windows: Pre-installed on most Windows systems. Object-oriented: Allows for complex data manipulation and interaction with APIs. Access to .NET Framework: Enables interaction with a vast library of classes and functions. Remoting capabilities: Execute commands on remote systems. Bypass security restrictions: Can be used to circumvent some security measures if not properly configured. Basic Syntax Cmdlets: Commands in PowerShell (e.g., Get-Process , Get-Service , Get-ChildItem ). P...

SQLDB Pentest

Pivoting for Red Teamers SQL Database & SQL Injection Pentesting Cheat Sheet SQL databases store crucial application data, and misconfigurations can make them vulnerable to SQL Injection (SQLi) attacks. This guide covers database enumeration, privilege escalation, and SQL injection techniques. Step 1: Identifying SQL Database Type Check the database type by sending payloads in the input fields or URL: ' OR 1=1 -- (MySQL, PostgreSQL, MSSQL) ' UNION SELECT 1,2,3 -- (Check column count) ' AND 1=CONVERT(int,@@version) -- (MSSQL Test) Observe the error messages for database identification. Step 2: Enumerating Database Tables & Columns Use SQL queries to extract database structure. For MySQL: SELECT table_name FROM information_schema.tables WHERE table_schema=DATABASE(); SELECT column_name FROM information_schema.columns WHERE table_name='user...

DAMN BASH

Bash Scripting: From Basic to Advanced Bash Scripting: From Basic to Advanced Bash (Bourne Again SHell) is a powerful command-line interpreter and scripting language commonly used in Linux and macOS environments. This post covers Bash scripting from basic commands to more advanced techniques. I. Basic Commands These commands are the building blocks of Bash scripting: Command Description ls Lists files and directories. cd Changes the current directory. pwd Prints the current working directory. mkdir Creates a new directory. rm Removes files or directories (use with caution!). cp Copies files or directories. mv Moves or renames files or directories. cat Displays file content. echo Prints text to the console. II. Variables Variables store data that can be used in your scripts: name="John Doe" echo "Hello, $name!" age=30 echo $((age + 5)) # Arithmetic operations III. Input/Output Redirection Redirect input an...