var unreadAlerts = '1';
You have one unread private message from dkota titled Welcome to the Forum!

Python Fundamentals
#1
 
Introduction
Hello everyone. This post is for the newbies who wanna get into hacking/programming stuff. You probably already heard that Python is one of the simplest and most useful programming languages to learn. It can be used to automate your tasks, make quick program and even creating your own hacking tools. So this thread will teach you most fundamental basics of this beautiful language. We won't go over setup and other shit. I hope you're not braindead, just search it online. This thread will include a lot of examples because it's easier to learn that way (and teach as well lmao). Also at the end of this thread you will find little challenge to do with new skills you just learned.

Syntax – How can python understand you?
Syntax is the way you write your code so programming language can understand you. In our case it's Python.
Main thing to remember:
Indentation (Tabs)
Python unlike other programming languages uses indentation not only for the readability. It's indicates a block of code.
Code:
if 2 == 2:
    print("Hello Onniforums!")
# this would work
You have to use at least 1 space, but usually its up to programmer. Just remember to use same amount of spaces each time.
Code:
if 2 == 2:
 print("Hello Onniforums!")
# this would work

if 2 == 2:
print("Hello Onniforums!")
# this wouldn't work

Comments – The way to make notes in your code.
Now, you will probably want to leave some comments in your code, so you don't forget what each function does, or you want to show your code to your friend. We can use # to indicate start of a comment.
It's important to notice, that if you want to have a BIG comment, you gotta use ''' to open and to close it.
Code:
# this is a comment
print("Hello Onniforums!")  # this is also a comment

'''
and this is a comment
and this is a comment
and this is a comment
'''

Variables – Name for your value.
Imagine you wrote a 5000 lines of python code, and you suddenly realize "Oh shit, instead of 'malware.exe' (that u put in like 300 lines of code) i typed 'mawlare.exe'". Now you're fucked, you have to change "mawlare.exe" to "malware.exe" manually 300 times. For this not to happen next time we use variables. Simply put a name of your variable, for example "file", and declare a text or a number to the variable by using "=".
Code:
file = "Malware.exe"
quantity = 1

print(file)
# will print out Malware.exe
If you wonder why some things declared differently, we gonna talk about data types soon.

Data types - 69 and 69.00 are not the same.
For now we just gonna discuss basic data types. Let's start.
  1. str is a text type. I mean it's pretty obvious what it does.
  2. int is a numeric type. It's used to declare simple number like 69.
  3. float is a little more complicated numeric type. It's used to declare numbers like 69.01000101
  4. list is a list of values. It can contain any value, like str and int at the same time.
  5. bool can be used to return True or False values.
Code:
animal = "Dog" # str
quantity = 69 # int
price = 12.99 # float
food = ["Apple", "Chicken", "Dog"] # list
bought = True # bool

Type conversion – How to turn number into text.
Now when we know about data types, we might need to turn it into another data type.
To do this, we can use str(), int(), float() and so on.
Code:
userinput = 69  # lets imagine that user entered number 69 (it will be int)

print("User input is " + userinput)
# ^ this code wouldn't work because we cant print int with str (text)


userinput = 69

print("User input is " + str(userinput))  # str() made userinput a text, so now we can print it

Operators - Built-in calculator inside a programming language.
WOW! We have to multiply 69 by 69, hard asf right? Well not really, because we can use operators to do this task.
Some basic operators: + (addition), - (subtraction), * (multiplication), / (division).
Code:
a = 1
b = 4
c = 13

x = a + b + c
print(x)  # will print 18 (1+4+13)

x = (a * b - c) / b
print(x)  # will print -2.25 ((1 * 4 - 13) / 4)

Constants – Make sure you don't change it.
Alright... we don't have syntax for constants in Python... But we have special rule to declare constants! Just make variable name in capital letters only and change spaces with _
Code:
# Program to calculate circumference of a circle

radius = 8
PI = 3.14  # constant
circumference = 2 * PI * radius  # formula to calculate circumference

print(circumference)  # will print out 50.24 (2 * 3.14 * 8)

Challenge
Try to run following code and fix errors!
Code:
# Shopping cart challenge

# Declaring variables and constants
item_price = "15,99"
quantity = "3"
discount_rate = "0,1"  # 10% discount
TAX_RATE = "0,05"  # 5% tax

# Calculating final price with taxes and discount
 subtotal = item_price * quantity  # total price without tax and discount
 tax = subtotal * TAX_RATE  # tax amount
 discounted = subtotal - (subtotal * DISCOUNT_RATE)  # total price with discount
 finalprice = discounted + tax  # total discounted price with tax

# Printing out final price
print("Final price: $" + float(finalprice))
Conclusion
I see you scrolled down here... It means you now either know about syntax, comments, variables, data types, type conversions, operators, constants and solved a challenge OR you just skipped everything. Any way, i hope this thread will gain attention, and if so - i will continue making these basic ass tutorials. Wanna see your feedback down below, thank you for reading this!

If you found something wrong in this thread, please tell me!
Do you want to continue? [Y/n]
Report


Quick Reply
Message
Type your reply to this message here.





Users browsing this thread: purely_cabbage
var thread_deleted = "0"; if(thread_deleted == "1") { $("#quick_reply_form, .new_reply_button, .thread_tools, .inline_rating").hide(); $("#moderator_options_selector option.option_mirage").attr("disabled","disabled"); }