Python Cheat Sheet
Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general purpose language, meaning it can be used to create a variety of different programs and isn’t specialized for any specific problems. This versatility, along with its beginner-friendliness, has made it one of the most-used programming languages today. A survey conducted by industry analyst firm RedMonk found that it was the most popular programming language among developers in 2020. Python has many advantages.
Python is:
- Easy to Read, Learn and Write
- Improved Productivity
- Interpreted Language
- Dynamically Typed
- Free and Open-Source
Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
x = 5
y = "John"
print
(x)
print
(y)
5
John
Variables do not need to be declared with any particular type, and can even change type after they have been set.
x = 69
# x is of type int
print
(x)
x = "Sally"
# x is now of type str
print
(x)
69
Sally
An attribute of data which tells the compiler or interpreter how the programmer intends to use the data.
Some Data types are:
- Boolean
- String
- None
- Int
- Float
Python supports the usual logical conditions from mathematics:
- Equals:
a == b
- Not Equals:
a != b
- Less:
a < b
- Less than:
a <= b
Loops are bit of code that run other code untill a condition is met
Python has two primitive loop commands:
while
loopsfor
loops
The while loop
With the while
loop we can execute a set of statements as long as a condition is true.
The for loop
A for
loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
Loops can be exited using break
.
In Python a function is defined using the def
keyword:
def
my_function:
print
("Hello from a function"
)
There are many great uses for functions. They can reduce repetition in code and make it easier to read