Learn Python in 10 Minutes or Less!

Photo by David Clode on Unsplash

Learn Python in 10 Minutes or Less!

[Important]

There is a lot of code written in here and much less text. I know it is overwhelming to see so much code when you don’t even understand the language.

But I have explained everything in each code block itself via comments. Reading line-by-line will help you a lot. And if there’s still anything that you feel is not properly explained here, be sure to comment and I’ll get back asap.

Alsop, try to type each command yourself instead of copying from here. You’ll learn a lot this way.

[Disclaimer]

In no way this article will make you a master of Python, but you will get a lot of understanding of how it works and help you get started.

Hello World

print("Hello World")

#Can be done like this as well
print("Hello", "World")


# Note: Anything after a hashtag is a comment and does not execute

Data Types

Integer

a = 5
b = 10

print(a)     #prints 5
print(b)     #prints 10
print(a, b)  #prints 5 10

Float

a = 1.5

# to convert into integer
int(a) # returns 1

String

ppytext = "The quick brown fox"

len(text) #returns length of text


a = "Apple"
b = "Banana"

c = a + b
d = a + " " + b

print(c) # prints: AppleBanna
print(d) # prints: Apple Banna

# [Advanced] String Formatting

a = "My name is {}".format("Devashish")
print(a) #prints: My name is Devashish

name = "Devashish"
b = f"My name is pp{name}"
print(b) #prints: My name is Devashish

Some advanced use cases with strings

# Split a string

text = "The quick brown fox"
word_list = text.split(" ") #splits text by spaces

print(word_list) #prints ['The', 'quick', 'brown', 'fox']


# join a list of strings
word_list = ['The', 'quick', 'brown', 'fox']
text = " ".join(word_list) # joins all words with spaces

print(text) #prints 'The quick brown fox'

# replace characters from string
text = "My name is Devashish"

new_text = text.replace("Devashish", "Bob")

print(new_text) #prints: My name is Bob

Boolean

a = True
b = False

List

# define a list 

num_list = [1, 2, 3, 4]
# indexing starts at 0
# i.e 
# 0th element => 1
# 1st element => 2
# and so on

#accessing elements by index
print(num_list[0]) # prints 1
print(num_list[3]) # prints 4

print(print(num_list[-1])) # prints last element i.e 4

fruits = ["apple", "banana", "orange"]

# len() returns number of elements in the list
len(num_list)

# append() adds a new element with value 5 at the end of the list
num_list.append(5)

Some advanced list operations

# [Advanced] how to slice a list
num_list = [1, 2, 3, 4, 5]

print(num_list[0:3]) #prints [1,2,3]
print(num_list[2:3]) #prints [3]
print(print(num_list[0:-1])) #prints [1,2,3]

# [Advanced] remove the last element
num_list.pop()

# [Advanced] remove an element by index
num_list.pop(3) #removes 4 (3rd element)

# [Advanced] List can also be defined like this
mylist = [x for x in range(3, 8)]

print(mylist) # prints [3,4,5,6,7]

Dictionary

# defining a dictionary
age = {"Bob": 25, "Alice": 24, "John": 28}

print(age["bob"]) #prints 25

#to reassign value
age["Alice"] = 23

#to add new element
age["Peter"] = 22

Sets

fruits = set()
fruits.add("apple")  #{'apple'}
fruits.add("banana") #{'apple', 'banana'}
fruits.add("orange") #{'apple', 'banana', 'orange'}

# adding an existing element won't make a difference
fruits.add("apple") #{'apple', 'banana', 'orange'}

# to remove an element from a set
fruits.remove("banana") #{'apple', 'orange'}

# to remove the first element added
fruits.pop() #also return the element i.e. apple

#to print the lenght of the set
print(len(fruits))

Operators

ppp# Additiom (+)

a = 2
b = 3
c = a + b
print(c) #prints 5

# Subtraction (-)

a = 6
b = 2
c = a - b
print(c) #prints 4


# Multiplication (*)
a = 2
b = 3
c = a * b
print(c) #prints 6
pp
# Division (/)
a = 6
b = 3
c = a / b
print(c) #prints 2

# Remainder (%)
a = 5
b = 2
c = a % b
print(c) #prints 1

Another set of operators that you’d mostly use in conditional statements are mentioned below:

# equals to (==), not equals to (!=)
a = 5
a == 5 # returns True
a == 6 # returns False
a != 5 # returns False
a != 6 # returns True

# greater than(>), greater than equals to (>=)
5 > 3   # returns True
4 > 4   # returns False
4 >= 4  # returns True

# less than (<), less than equal to (<=)

2 < 5   # returns True
4 < 4   # returns False
4 <= 4  # returns True

Loops

For loop

# iterate over a list
numbers = [1,2,3,4,5]
for i in numbers:
    print(i)

#iterate over a dictionary
age = {"Bob": 25, "Alice": 24, "John": 28}
for name in age:
    print(name, age[name])

#iterate over a range, below code prints numbers from 0 to 9
for i in range(0, 10):
    print(i)

While Loop

#this will print numbers from 0 to 9 and the loop will stop at i=10
#remember to increment i after every run

i = 0
while(i < 10):
  print(i)
  i = i+1 #can be replaced by  i+=1 as well

Conditionals (if, elif, else)

a = 5

if a<0:
    print("a is negative")
elif a>0:                    #this will execute for a = 5
    print("a is positive") 
else:
    print("a is zero")

Functions

pp# to define a function, use keyword `def`
def sum(arg_1, arg_2):
    return arg_1 + arg_2

#call a function
sum(5, 6) #returns 11

Classes

# to define a class use `class` keyword
# Class name generally has the first letter Capital
# __init__ function is used for class initialisation

class Cars:
    def __init__(self, model, year):
        self.model = model
        self.year = year

    def get_car(self):
        return f"Car Model: {self.model}, Car launch year: {self.year}"


# How to use a class

car_1 = Car("RB16a", 2016)
car_2 = Car("RB16b", 2017)

print(car_1.get_car()) # prints: Car Model: RB16a, Car launch year: 2016

Imports

This is a major part of development with Python. You’ll need to import a lot of inbuilt as well as external libraries in your code. Below is an example of how you’d do that.

import datetime

date_today = datetime.datetime.now()

print(str(date_today))p
print(date_today.year)

User Input

name = input("Enter your name: ") #Enter Name after executing this

age = input("Enter Age: ") #Enter Age after executing this

print(f"Age of {name} is {age}")

While there’s a lot more to Python than what is mentioned in this article, you will have a good understanding of the basics and now you can move to advanced stuff. I suggest practicing by yourself and try building a project.

Follow me for more such content.