-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b5b26b
commit d81dfba
Showing
12 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This is just printing stuff | ||
|
||
print("Hello World") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 == | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |