Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

🏗️ Data Structures

🔠 Variables
Basic method to assign values to a variable 👇🏻
name = 'Rafeh Qazi'
age = 25

>>> print(name)
>>> print(age)

Rafeh Qazi
25

More ways to assign values to variables 👇🏻


width, height = 400, 500

>>> print(width)
>>> print (height)

400
500

📥 Taking Inputs
You have to assign the value of your input directly to a variable.

your_name = input('Please enter your name:')


print('Hi ' + your_name)

>>> Please enter your name:


>>> Rafeh

Hi Rafeh

🐍 Data Types
Untitled 1
🪢 Strings
Strings can be etither words or sentences (and even numbers somtimes)

name = 'Rafeh'
country = 'USA'
city = 'Los Angeles'

💯 Numbers
There are two number data types 👇🏻
🧱 Integers
age = 28
weight = 146

🏖️ Float
temperature = 65.3
longBeachToLAX = 20.7

💡 Booleans
Indicates whether somethin is true or false.

isMyNameQazi = true
doIHaveAPlaystation = false

📝 Lists
Similar to array in JavaScript, but Python uses the word 'Lists'.

fruits = ['🍎', '🍊', '🍌', '🍇', '🍑']

👨🏻‍🔬 Math Operators


Untitled 2
Math Operators are used to do mathematical calculations within your application.

➕ Addition
Adding two or more numbers.

print(5 + 5)
>>> 10

num1 = 6
num2 = 7

print(num1 + num2)
>>> 13

➖ Subtraction
Subtracting two or more numbers.

print(6 - 2)
>>> 4

num1 = 8
num2 = 3

print(num1 - num2)
>>> 5

✖️ Multiplication
Multiplying two or more numbers.

print(6 * 2)
>>> 12

num1 = 8
num2 = 3

print(num1 * num2)
>>> 24

➗ Division
Untitled 3
Dividing two or more numbers.

print(6 / 2)
>>> 3

num1 = 24
num2 = 6

print(num1 / num2)
>>> 4

🌫 Floor Division
Rounds the result down to the nearest whole number

print(5 // 2)
>>> 3

📈 Exponentiation
Multiplies the first number with an exponent equal to the second number.

print(6 ** 2)
# similar to 6*6
>>> 36

num1 = 2
num2 = 6

print(num1 ** num2)
# similar to 2*2*2*2*2*2
>>> 64

🍪 Modulus
Gives the remainder from the division.

print(5/2)
>>> 1

Untitled 4
print(14/3)
>>> 2

🤔 Comparison Operators
Comparison Operators are used to compare variables, etc. within your application.

1️⃣ Is Equal to '=='


Compare if two values are equal and return a boolean.

qaziAge = 28
davidAge = 23

print(qaziAge == davidAge)
>>> False

internetSpeedInLA = 100
internetSpeedinDubai = 100

print(internetSpeedInLA == internetSpeedinDubai)
>>> True

2️⃣ Is Less than '<'


qaziAge = 28
davidAge = 23

print(davidAge < qaziAge)


>>> True

longBeachWeather = 66
sanFranciscoWeather = 57

print(longBeachWeather < sanFranciscoWeather)


>>> False

3️⃣ Is Greater than '>'


qaziAge = 28
davidAge = 23

Untitled 5
print(davidAge > qaziAge)
>>> False

longBeachWeather = 66
sanFranciscoWeather = 57

print(longBeachWeather > sanFranciscoWeather)


>>> True

4️⃣ Is Less Than or Equal '<='


print(5 <= 5)
>>> True

print(4 <= 5)
>>> True

print(6 <= 5)
>>> False

5️⃣ Is Greather Than or Equal '>='


print(6 >= 6)
>>> True

print(7 >= 6)
>>> True

print(5 >= 6)
>>> False

📚 Dictionaries
def introducer():
person = {
'name': 'Qazi',
'shirt': 'Black',
'laptop': 'Apple',
'phone_number': '224-123-3456',
'assets': 100,

🍎', '🍌', '🍊'],


'debt': 50,
'favoriteFruits': ['
'netWorth': lambda: person['assets'] - person['debt']

Untitled 6
}

person['shirt'] = 'Orange'

👋
person['assets'] = 1000
print(f" Hi my name is {person['name']},
👕 👨‍💻
💰 🥝
\\n I am wearing a {person['shirt']} shirt, \\n and the laptop I use to code is an
{person['laptop']}, \\n and my net worth is ${person['netWorth']()} USD, \\n My favorit
e fruits are {person['favoriteFruits']}")

>>> introducer()

`👋 Hi my name is Qazi,
👕 I am wearing a Orange shirt,
👨‍💻 and the laptop I use to code is an Apple,
💰and my net worth is $950 USD,
🥝My favorite fruits are ['🍎', '🍌', '🍊']`

🤔 Conditionals (if else)


def weather_to_emoji(weather: str) -> None:
if weather == 'rain':

print(' ')
elif weather == 'cloudy':
print('🌩️')
elif weather == 'thunderstorm':
print('⛈️')
else:
print('😎')

>>> weather_to_emoji('rain')
'☔'

🍬 For Loops
Example #1: Double Numbers in a List

def double(numbers: list) -> list:


result = []
for number in numbers:
result.append(number * 2)
return result

>>> double([1, 2, 3, 4, 5])


[2, 4, 6, 8, 10]

Untitled 7
Example #2: Find the Max in a List

def find_max(numbers):
current_max = numbers[0]
for number in numbers:
if number > current_max:
current_max = number

return current_max

>>> find_max([1, 2, 3, 10, 17, 4, 5, 6])


17

Example #3: Find the Frequency of Words in a Phrase

def word_frequency(phrase):
result = {}
words = phrase.split()
for word in words:
if word not in result:
result[word] = 1
else:
result[word] += 1

return result

>>> word_frequency('I love Batman because I am Batman')


{'I': 2, 'love': 1, 'Batman': 2, 'because': 1, 'am': 1}

SPECIAL BUILT-IN FUNCTIONS with Python

>>> sum([1, 2, 3])


6
>>> len([1, 2, 3])
3
>>> max([1, 2, 3])
3
>>> max([1, 2, 3, 10, 5, 7])
10
>>> min([1, 50, -7, 337])
-7

Untitled 8
🏗️ Functions
1️⃣ Basic Function without any arguement.
def say_my_name():
print('Rafeh Qazi')
print('John')
print('Kara')
print('Sam')

say_my_name()
>>> Rafeh Qazi
>>> John
>>> Kara
>>> Sam

2️⃣ Function with one arguement.


def say_my_name(name):
print(name)

say_my_name('Rafeh')
>>> Rafeh

3️⃣ Function with multiple arguements and a default arguement


def greeting(name, greet='aloha'):
# Function greeting() takes in 2 arguments, 'name' & 'greet' and it greets the user.

>>> greeting('aloha', 'Qazi')


"👋 aloha Qazi!"
print(f"👋 {greet} {name}!")

# Positional arguments 👇🏻
# greeting('Qazi', 'hello')

# Named arguments 👇🏻
# greeting(greet='Hi', name='Qazi')

Untitled 9
Untitled 10

You might also like