Learn Python in 4 Minutes

Gamze Yılan
Nerd For Tech
Published in
4 min readNov 20, 2022

--

Here’s a cheatsheet to boost-start you working with Python. Just a 4 minute read and you’re good to go hands-on!

Getting Started

Python is generally preferred for that it’s rather simple to learn. It’s used for developing websites and software, task automation, data analysis and data visualization.

It supports multiple programming paradigms like object-oriented, structured and functional programming.

Variables and Strings

You can define a variable without giving it a type as:

var1 = “Hello world!”

var2 = 354

You can print a string as:

print(“Hello world!”)

Note: Variable names cannot start with a number, they can only contain A-z, 0–9, and _ and they are case sensitive.

Lists

You can define a list as:

list = [“apple”, “orange”, “banana”]

Where the first item and the last item within a list can be called as:

list[0] = “apple”;

list[-1] = “banana”;

You can loop through a list, work within a number range and add an item to a list as:

numList = []

for n in range(3, 6):

numList.append(item) // numList = [ 3, 4, 5 ]

Note: Range works for the lesser limit but stops right before the greater limit.

You can slice the list and get items before/after an index as:

numList[:1] // returns items before the index 1 = [3]

numList[1:] // returns items at & after index 1 = [4, 5]

You can delete a value by it’s index as:

del numList[1]

Or you can delete a value that you don’t know the index of by using the value itself as:

numList.remove(4)

You can pop the last value within a list and even assign it on a variable as:

lastNum = numList.pop() // lastNum = 5

A Tuple is a variable type in Python that is just like a List and is used to store multiple values within a single variable, but are unchangeable.

myTuple = (“apple”, “orange”, “banana”)

Conditions

Classical conditions work with Python as well:

x == 4, x ≤ 4, x ≥4, x<4, x>4, x != 4 …

There are also list conditions:

“apple” in list // returns true since “apple” exists in list

“orange” not in list // returns false since “orange” exists in list

You can set boolean values as:

myVar = False // watch the capital letter of False

If statements in Python look like:

if age ≤ 12:

isChild = True

else if 12< age <18:

isTeen = True

else:

isAdult = True

Dictionaries

You can keep key-value pairs as variables within Python. These kind of variables are called dictionaries. A dictionary can hold different data types as the value for keys.

user = { ‘name’ : ‘Jane Doe’, ‘age’ : 30}

print(user[‘name’]) // prints Jane Doe

user[‘phone’] = ‘5554433’ // adds key ‘phone’ with the value ‘5554433’ to the user dictionary

You can loop through a dictionary as:

fav_fruit = { ‘Jane’ : ‘apples’, ‘John’ : ‘oranges’ }

for name, fruit in fav_fruit.items():

print(name + “ loves ” + fruit)

// prints “Jane loves apples” and “John loves oranges”

Or you can loop through only the keys or the values within a dictionary as:

for name in fav_fruit.keys():

print(name + “ loves fruit!”) // prints “Jane loves fruit!” and “John loves fruit!”

You can get an input as:

name = input(“What’s your name?”)

Note: The input is by default a string in Python. So if you’re looking to get an input of, let’s say, an int you should do as:

age = input(“How old are you?”)

age = int(age)

While Loops

You can run a while loop as:

age = 15

while age < 18

print(“User is not an adult yet!”)

Functions

You can define and call a function as:

def add_numbers(x , y):

return x + y

sum = add_numbers(3 , 5)

print(sum) // prints 8

Classes

You can define a class and create instances of it as:

class Person:

def __init__(self, name, age):

self.name = name

self.age = age

person1 = Person(“Jane”, 30)

print(person1.name)

Files

You can read from or write to files with Python as:

fileName = ‘employees.txt’

with open(fileName) as file_object:

file_object.write(‘John Doe’) // writes “John Doe” to the file

lines = file_object.readLines()

for line in lines:

print(line) // prints “John Doe”

Exceptions

You can handle exceptions as:

prompt(“Enter a number”)

try:

num = input(prompt)

expect valueError:

print(“Value invalid. Try again.”)

else:

print(“Thank you.”)

Conclusion

Altough this article is a great way to get a quick start and start working hands-on, Python can offer many more than these basic functions and it comes with a great documentation so make sure to check it out every now and then. Do keep in mind that Python comes with amazing third party modules based on whatever you’re going to use it with and excels via them.

--

--