Skip to content

Commit

Permalink
added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
junaidrahim committed Apr 8, 2022
1 parent 8b5b26b commit d81dfba
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 01 Basics/01_hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is just printing stuff

print("Hello World")

15 changes: 15 additions & 0 deletions 01 Basics/02_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# There are many ways to think about variables
# - containers for values
# - aliases for values
# - placeholders for values that change

x = 10
y = 30
z = True
f = 3.14151

print(x)
print(y)

x = "This is a string"
print(x)
24 changes: 24 additions & 0 deletions 01 Basics/03_operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# You can do a variety of operations with values and variables
# - Add, Subtract, Multiply, Divide, Exponentials
# - Remainder, Absolute

x = 22
y = 7

print("x + y =", x + y)
print("x - y =", x - y)
print("x * y =", x * y)
print("x / y =", x / y)

a = 11
b = 3

print("a ^ b = ", a**b)
print("a % b = ", a % b)
print("a // b = ", a//b)


# Logical Operators
# - and, or & not keywords
# - Difference between is and ==

21 changes: 21 additions & 0 deletions 01 Basics/04_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Python is dynamically and strongly typed,
# the types of the variables are figured out by the interpreter
# but you can't just mix up types when working with them

s = "GDSC KIIT" # string
a = 100 # int
b = 2.3 # float
d = 10e5 # float again, this is 10 * 10^5

# What happens when we try to add a string to an int ?

print(s + a) # ??


# You can transform the types of variables

string_a = str(a)
print(string_a, type(string_a))

int_b = int(b)
print(int_b, type(int_b))
5 changes: 5 additions & 0 deletions 02 Input & Output/01_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Accepting input from the user

x = input("Enter your Name: ")
print(x)
print(type(x))
16 changes: 16 additions & 0 deletions 03 Loops, Arrays, Dicts/01_loops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# A loop is just to repeat a block of code in a controlled way


# For loop
for i in range(1,10):
print("Hello World")

# loops in python work on an iterator based model



# While loop
i = 0
while i < 10:
print("Hello World")
i += 1
41 changes: 41 additions & 0 deletions 03 Loops, Arrays, Dicts/02_arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Arrays/Lists are dynamically sized arrays that you can
# use to store homogeneous and heterogeneous data

a = [1, 2, 3]
b = ["python", 123, "allows", 3.45, "this"]

garage = ["ferrari", "mercedes", "aston martin"]

for car in garage: # you can iterate over arrays
print(car)

tic_tac_toe = [
["x", "o", "o"],
["o", "x", "0"],
["o", "o", "x"]
]

for line in tic_tac_toe:
for move in line:
print(move, end=" ")
print()


# Slicing Arrays
# Python has a wonderful set of features to deal with arrays

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(a[3])
print(a[-1])
print(a[-3])
print(a[2:4])
print(a[3::])
print(a[::4])
print(a[::-1])

# List comprehensions - Dynamically building arrays


squares = [x**2 for x in range(0, 10)]
print(squares)
Empty file.
16 changes: 16 additions & 0 deletions 04 Functions OOP/01_Functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Functions, there can be multiple ways of thinking what
# really is a functions - reusability or transformations

def say_hello_twice():
print("Hello")
print("Hello")

say_hello_twice()


# Transforming an input to output

def square(x):
return x**2

print(square(10))
33 changes: 33 additions & 0 deletions 04 Functions OOP/02_Classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# OOP, this is a vast subject, but I'll try to give a gist of it

class Player:
def __init__(self):
self.position = 0
self.bullets = 10
pass

def walk(self):
print("walk")
self.position += 1

def shoot(self):
self.bullets -= 3
print("pew pew pew")

def reload(self):
self.bullets = 10

def print_stats(self):
print("Bullets:", self.bullets)
print("Position:", self.position)


p = Player()
p.walk()
p.walk()
p.shoot()
p.shoot()
p.print_stats()
p.reload()
p.walk()
p.print_stats()
8 changes: 8 additions & 0 deletions 06 Dealing with Files/01_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Reading a file

f = open("./zen.txt", 'r').readlines()

print(f)

for line in f:
print(line)
21 changes: 21 additions & 0 deletions 06 Dealing with Files/zen.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

0 comments on commit d81dfba

Please sign in to comment.