Skip to content

Instantly share code, notes, and snippets.

@misostack
Last active June 11, 2024 14:14
Show Gist options
  • Select an option

  • Save misostack/0f3c02d1d6f00cf4b60c205a3f1d71ec to your computer and use it in GitHub Desktop.

Select an option

Save misostack/0f3c02d1d6f00cf4b60c205a3f1d71ec to your computer and use it in GitHub Desktop.
Python

Python

Install

MACOS

brew install pyenv
pyenv versions
pyenv install --list | grep 11
brew install xz # fixed missing lib when install python using pyenv
pyenv install 3.11.4
pyenv global 3.11.4
  • Add "eval "$(pyenv init --path)" to "/.zprofile"
# Setting PATH for Python 3.11
# The original version is saved in .zprofile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}"

# Setting PATH for pyenv
eval "$(pyenv init --path)"
which python
which python3
which pip

Install pipenv

pip3 install --user pipenv

.zprofile

# Setting pip packages
export PYTHONUSERBASE="$HOME/.local"
PATH="$PYTHONUSERBASE/bin:${PATH}"
source ~/.zprofile
which pipenv

Use pipenv

# activate
pipenv shell
# install dependencies
pipenv install

Keywords and usecases

1.False : boolean value

2.await

3.else

4.import

5.pass

6.None

7.break

8.except

9.in

10.raise

11.True

12.class

13.finally

14.is

15.return

16.and

17.continue

18.for

19.lambda

20.try

21.as

22.def

23.from

24.nonlocal

25.while

26.assert

27.del

28.global

29.not

30.with

31.async

32.elif

33.if

34.or

35.yield

@misostack

misostack commented Aug 3, 2023

Copy link
Copy Markdown
Author

Python development for beginner

REPL

Read, Evaluate, Print, Loop

Comments

image

String

https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
image

Numbers

https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
image

Boolean

image

Variables

image

Sequence

List : order sequence of items

image

image

image

Range:
image

Tuple:
image

Dictionary:
image

@misostack

Copy link
Copy Markdown
Author

Python Essentials

Data types

image

image

image

@misostack

Copy link
Copy Markdown
Author

image

@misostack

Copy link
Copy Markdown
Author

image

@misostack

misostack commented Aug 4, 2023

Copy link
Copy Markdown
Author

@misostack

Copy link
Copy Markdown
Author

image

@misostack

misostack commented Aug 4, 2023

Copy link
Copy Markdown
Author

OOP

Basic class

class Car:
	"""
	Docstring descibe the class
	"""

	def __init__(self, engine, tires):
		"""
		Docstring describe the method
		"""
		self.engine = engine
		self.tires = tires

	def description(self):
		print(f"A car with an {self.engine} has {self.tires}")


class Tire:
	"""
	Tire represents a tire that would be used with an automobile
	"""

	def __init__(self, tire_type, width, ratio, diameter, brand='', construction='R'):
		self.tire_type = tire_type
		self.width = width
		self.ratio = ratio
		self.diameter = diameter
		self.brand = brand
		self.construction = construction


	def __repr__(self):
		"""
		Represent the tire's information in the standard notation
		"""
		return f"{self.tire_type}{self.width}{self.ratio}{self.diameter}{self.brand}{self.construction}"

Composition

image

Doctest

image

python-headfirst % python3 -m doctest -v tire.py
import math

class Tire:
	"""
	Tire represents a tire that would be used with an automobile
	"""

	def __init__(self, tire_type, width, ratio, diameter, brand='', construction='R'):
		self.tire_type = tire_type
		self.width = width
		self.ratio = ratio
		self.diameter = diameter
		self.brand = brand
		self.construction = construction


	def circumference(self):
		"""
		The cincumference of the tire in inches

		>>> tire = Tire('P', 205, 65, 15)
		>>> tire.circumference()
		80.1
		"""
		side_wall_inches = (self.width * (self.ratio/100)) / 25.4
		total_diameter = side_wall_inches * 2 + self.diameter
		return round(total_diameter * math.pi, 1)


	def __repr__(self):
		"""
		Represent the tire's information in the standard notation
		"""
		return f"{self.tire_type}{self.width}{self.ratio}{self.diameter}{self.brand}{self.construction}"

@misostack

Copy link
Copy Markdown
Author

CoderByte

CoderByte Binary Gap

image

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(N):
    # Implement your solution here
    # 1 1 0 0 0 0 1 1 0 0 0 1
    binary_gap = 0
    if N <= 0:
        return binary_gap
    # otherwise convert to binary number
    binary_str = str(bin(N)).replace('0b','')
    # binary gap start index
    binary_gap_sid = None
    # binary gap end index
    binary_gap_eid = None
    cid = 0
    for c in binary_str:
        if c == '1':
            if binary_gap_sid == None:
                binary_gap_sid = cid
            else:
                if binary_gap_eid == None:
                    binary_gap_eid = cid
                    active_range_gap = binary_gap_eid - binary_gap_sid - 1
                    # reset range
                    binary_gap_sid = binary_gap_eid
                    binary_gap_eid = None
                    if active_range_gap > binary_gap:
                        binary_gap = active_range_gap

        cid +=1
    return binary_gap


solution(1041)

@misostack

Copy link
Copy Markdown
Author

Pybase

image

@misostack

Copy link
Copy Markdown
Author

image

@misostack

Copy link
Copy Markdown
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment