Versions Compared

Key

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

Table of Contents

Basics

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


Input and Output

Code Block
first_name = input("What is your name? ")
print("Hello " + first_name)


Type Conversions

Python has the following functions for type conversion:

  • int(),
  • float(),
  • bool(),
  • str()


Code Block
#Convert String to int
birth_year = input("What is your birth year: ")
age = 2021 - int(birth_year)
print(age)

# or another way
birth_year = int(input("What is your birth year: "))
age = 2021 - birth_year
print("Age: " - str(age))


Strings

When we define a string, it becomes an object of type string. We can call any methods of the object string.

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


Code Block


Cheat Sheet


References

ReferenceURL


Python Cheat sheet for hackers and developers


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


...