Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Basics

Comments

Comments start with a pound(#) sign.

Code Block
# define a number 
time_elapsed_s = 12.34


Variables

Code Block
# define a number 
time_elapsed_s = 12.34

#define a string
first_name = "John"

#define a boolean - True/False
is_online = True

...

Code Block
course = "Course Name"
index = course.find("u")
print(index)

#or to get True/False if an substring exists using IN
found = "Name" in course
print (found)

Arithmetic Operators

OperatorDescriptionExample
+plus

10 + 3

= 13

-minus

10 - 3

= 7

*multiply

10 * 3

= 30

/divide - returns float

10 / 3

= 3.333

//divide (whole number)

10 // 3

= 3

%modulus - remainder of division

10 % 3

= 1

**exponent

10 ** 3

= 1000

+=, *=, ...


Augmented Assignment Operators

x+=3

x=x+3


Comparison Operators

OperatorDescription
>Greater than

>=

Greater than or equal
<Less than
<=Less than or equal
==Equal
!=Not Equal


Logical Operators

OperatorDescriptionExample
andlogical and

price =20

print( price >10 and price < 30 )

orlogical or

price =20

print( price >10 or price < 30 )

not

price = 20

print (not price >10)


If Statements

If statements use indentation instead of curly brackets in Java.

Code Block
temperature = 25
if temperature > 30:
    print("It's a hot dat")
    print("Drink plenty of water")
elif temperature > 25:
    print("It's a nice day")
else:
    print("It's a cold day")
print("Done")	


While Loops

Code Block
i=1
while i<=1_000: 
	print(i)
	i=i+1

Notice in the above example we used an underscore to make reading the integer 1000 easier to read.


Cheat Sheet


References

ReferenceURL
Python for Beginners - Learn Python in 1 Hourhttps://www.youtube.com/watch?v=kqtD5dpn9C8

Python Cheat sheet for hackers and developers

https://hakin9.org/python-cheat-sheet-for-hackers-and-developers/


...