Python Stuff
Python Stuff
Python Stuff
Syntax
how to make python code actually work with python
Python Stuff 1
Python lacks a way to define variables via command.
Variables
As mentioned before, there is currently no way to declare a
variable via command. it's simply there the instant it is
defined
You can put variables inside strings using curly braces with
their name inside.
Python Stuff 2
Variable names have a set few rules which go as follows:
words = ["Generic","Something","fake"]
#said variable can be converted into other ones as show belo
w:
x,y,z = words
#x, y, and z will each take one of the terms defined as words
at the top.
x = "this"
y = "is"
z = "a test."
#defines the variables
print(x+y+z)
Python Stuff 3
#the plus tells python to print all those individual va
riables on the same line.
Data types
Python has a few data types built in out the gate. they are
the following:
Python Stuff 4
Once again, you can use the type() function to figure out
what data type a variable is
int=integers,
float=decimals
x = 10
y = 2.1
z = 1j
a = float(x)
print(a)
print(b)
print(c)
Casting
There are scenarios in which you have to specify what data
type a variable is. this is where casting comes in
Python Stuff 5
Casting allows you to state what type of data is being
used in a variable. it is done like so:
x = int(1)
#using a function that has the name of the data type te
lls python that whatever is in the parantheses is of th
at data type and will function accordingly.
Strings
Strings is basically just text. it is surrounded by single
or double quotation marks
a = "Hello, World!"
print(a[1])
#the [1] tells it to print the 2nd letter in the string in
a
Python Stuff 6
words = "things are very well"
if "well" in txt:
#checks to see if the word 'well' is in the text
print("Yes, 'well' is present.")
You can use len() to have python tell you the length of a
string
You can use negative values to start the slice from the
end
Python Stuff 7
a = "amongus"
#the upper() function is attached to the next via a . so p
ython knows to apply the change to the info defined in "a"
print(a.upper())
a = "guud wurds"
#the first character in replace() is the character that wi
ll be changed and the second is the letter character it wi
ll be changed to
print(a.replace("u","o"))
#Python will print "good words" instead of the "guud word
s" that a is supposed to be.
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
#the curly brackets act as a placeholder
Python Stuff 8
its in
wow="Very\"good\"words"
print(wow)
#This will allow python to print "Very "good" words" as ot
herwise it would think you were starting and ending string
s over and over again
\\ Backslash (adds a \)
\n New Line
\r Carriage Return
\f Form Feed
Python Stuff 9
ones are:
Python Stuff 10
Returns True if all characters in the string are
isspace()
whitespaces
Python Stuff 11
strip() Returns a trimmed version of the string
Booleans
Booleans represent something either being true or false. They
are binary variables
print(10 > 9)
#this will either print true or false. Since 10 is greater th
an 9, it will return and print 'True'
a = 21
b = 33
if b > a:
print("b is greater than a")
#python will only print this if the boolean has been set to
'True'
else:
print("b is not greater than a")
Python Stuff 12
#python will only print this if the boolean has been set to
'False'
def myFunction() :
return True
print(myFunction())
#this will print 'True' as myFunction() has "return True"
under it in hierarchy
x = 200
#defines x for later
print(isinstance(x, int))
#begins by calling isinstance() and begins by introducing
the data it will evaluate and after the comma tells python
Python Stuff 13
what its checking for. In this case, it is checking to see
if the 200 in x is an integer.
Operators
Operators are things a python can preform on variables an values
+ Addition x + y
- Subtraction x - y
* Multiplication x * x
/ Division x / y
% Absolute Value x % y
** Exponentiation x ** y
// Floor division x // y
You can use “in” and “not in” to check if a specific value is
or isn’t in a value. It can return either the boolean “True”
or “False”
for x in y:
Python Stuff 14
#code, or something
Functions
Functions are blocks of code that only run when they are
called/told to
def function_name_here():
#code goes here
Lists
Lists are literally just lists of data types, typically strings
or numeric types although they can be any data type
lists are compatible with the len() function just like other
data types.
They are also compatible with type() too
you can still use brackets when calling the list to pick
certain items on the list. this is called indexing
Python Stuff 15
You can use negative indexing to make it start from the
end. this would mean [-1] would take the second to last
item in the list
listthing = ["uhhh","words","methinks","wahoo"]
print(listthing[2:4])
#this tells python to pick all the items in the list start
Tuples
Tuples are almost identical to lists except they cannot be
changed.
Set
sets are also like lists but are not in order and are not
changeable and aren’t indexed
Python Stuff 16
Dictionary
Disclaimer: in python 3.6 or older dictionaries weren’t in
order. as of 3.7, the are in order
beyond this, they contain similar properties to lists except
they cannot have duplicate members/items.
type() that tells you the data type of the given data
Python Modules
Python has a bunch of modules that carry out basic functions
built in such as:
Conditional Statements
Conditional statements involve requesting the computer check if
a certain requirement is or isn’t met
x = 1
y = 2
Python Stuff 17
#You begin with the actual "if" then insert the condition.
All if else statements end in ":" before you can add the c
ode block
if y > x:
print("x is in fact lesser than y")
for i in range(100):
print("real")
#this will print "real" 100 times. Range is used to se
t the number of times it does so and i is the variable to
make it work.
real = False
while real == False:
print("real is still false")
#this will keep printing "real is still false" until t
he variable real is no longer false which can be at any ti
me. Hence why its used for undefined variables
File Handling
Python Stuff 18
Random python facts
Not equal to is done using !=.
Python Stuff 19