Python is an object-oriented programming language. On many occasions, it has been quoted as an interpreted language, which uses interchangeable code modules. Python has an extensive library. Currently, this language is in high demand in the data science sector.
Programming through Python language can turn out to be a tedious task if one is not good at memorization. A Programmer needs to keep all things in mind related to languages for instance syntax, function, rules and their properties. A cheat sheet holds the key to make these complex tasks simple. In this article, we try to bring a helpful cheat sheet for Beginners of the Python programming language. This cheat sheet will guide you through variables, string, data types, and loops, etc. If you are looking forward to upgrading your skills in Python language you can join complete Web Development Bootcamp programs.
Cheat Sheet
Command Line
Write python syntax directly in the command line.
>>> print (“Hello, Percy”)
Hello, Percy! // is printed.
Execute Python Syntax
Create a python file on the server, use “.py” file extension, and run it in the Command Line:
Example
C:\ Users\Your Age> python file1.py
Python Indentation
The indentation term refers to the spaces at the start of a code line. In other programming languages, the indentation used in code is primarily for readability only. Python uses indentation to specify a block of code.
Example
if 6 > 2:
print(“Six is greater than two!”)
Variables
We specify variables in the code to temporarily store data in the computer’s memory. A variable is created the second you assign a value to it. Variables are not required to be declared with any particular type. Variables can even change type after they have been set.
Example:
x = 7
y = “Smith”
print(x)
print(y)
Built-in Data Types
Variables can basically store data of different types, and those different types can perform different things. Python has data types built-in by default, such as the categories listed below:
- Text Type: Str
- Numeric Types: Int, float, complex
- Sequence Types: List, tuple, range
- Mapping Type: Dict
- Set Types: Set, frozenset
- Boolean Type: Bool
- Binary Types: Bytes, bytearray, memoryview
You can get the data type of any particular object by using the type () function. Print the data type of the variable y:
Example
y = 5
print (type(y))
Setting the Data Type
In Python, the data type is set when the developer assigns a value to a variable.
Example:
- x = str (“Hi!”)
- x = int (20)
- x = float (20.5)
- x = complex (1j)
- x = list ((“apple”, “banana”, “cherry”))
- x = tuple ((“apple”, “banana”, “cherry”))
- x = range(6)
- x = dict (name = “Joe”, age= 31)
- x = bool (6)
- x = bytes (5)
- x = bytearray (5)
Python Casting
There can be times when you want to state a type on a variable. This can be done through casting. Python is an object-orientated language, and it uses classes to express data types, including its primitive types. Casting in python is done using constructor functions:
int () – constructs an integer number in the code from an integer literal, a string literal (providing the string represents a whole number), or a float literal (by rounding down to the previous whole number)
Example
d = int (2) // d will be 2
e = int (3.8) // e will be 3
float(): Constructs a float number in the code from a float literal, an integer literal, or a string literal (providing the string signifies a float or an integer)
Example
a = float (2) // a will be 2.0
b = float (3.8) // b will be 3.8
c = float (“4”) // c will be 4.0
str(): Constructs a string from an extensive kind of data types, that includes strings, integer literals & float literals.
Example
m = str (“s1”) // m will be ‘s1’
z = str (3) // z will be ‘3’
String Literals
String literals in python are shown by single quotation marks or double quotation marks.
‘Hello’ is the same as “Hello”.
You can show a string literal with the print () function.
Example
print(“Hello”)
print(‘Hello’)
Boolean Values
In programming you often want to know if an expression is true or false. Evaluate an expression in Python and receive either True or False. When you do a comparison of two values, the expression is evaluated and Python will return the Boolean answer in the end.
Print (10 > 9)
print (10 == 9)
print (10 < 9)
Python Operators
Operators are meant to perform operations on variables & values. The operators in python are divided into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Python If … Else
Python Conditions and If statements
Python supports the logical conditions from mathematics:
- Equals: y == z
- Not Equals: y != z
- Less than: y < z
- Less than or equal to y <= z
- Greater than: y > z
- Greater than or equal to y >= z
These conditions are used in several ways, such as mostly in “if statements” and loops.
An “if statement” is used by using the “if” keyword.
Python Loops
Python has two primitive loop commands:
- while loops
- for loops
The while Loop
With the while loop developers can execute a set of statements as long as the condition remains true. Check the syntax for the “While” loop given below.
i = 1
while i < 6:
print(i)
i += 1
Python “For” Loops
A “for” loop is used for iterating over a given sequence (it can be a list, a tuple, a set, a dictionary or a string). “For” keyword acts differently in other programming languages. “For” loop works like an iterator method in other programming languages. With the “for” loop we can perform a set of statements, once for each element in a list, set, tuple, etc.
Fruits=[“banana”, “cherry”]
for y in fruits:
print(y)
“For” loop does not primarily require the indexing variable in the code to set beforehand.
Python Functions
A function is a block of code that is run at the time of being called. You can pass parameters into a function. A function can return that parameter result in the end.
Python Arrays
Arrays
Arrays in the code can store multiple values in one single variable.
Example
Create an array containing different car names:
Cars = [“Volvo”, “BMW”]
Python Classes/Objects
A Class is the same as an object constructor, or like a “blueprint” to create objects.
Create a Class
The keyword class in the code should be used to create a class,
Example:
Create a class Class1, with a property named x:
class Class1:
x = 5
Create Object
Now we can use the class named Class1 to create objects.
Example
Create an object i.e. “h7”, & print the value of y:
h7 = MyClass()
print(h7.y)
This was the basic. Stay connected with us for more in-depth articles about Python.