Python Fundamentals

Blog post description.

PROGRAMMINGPYTHON

Daniel Afkhami-Ardekani

11/17/20241 min read

#Data Types
  • int = an integer

  • float = Floating point number

  • str = string, which is written in "" or ''

    • You can also use three quotes like this ''' some string ''' for longer strings, like if you have multiple sentences or paragraphs.

  • bool

  • list

  • tuple

  • set

  • complex

Classes > custom types

Specialized Data Types

  • None = the absence of value

#Operator precedence
  • Like PEMDAS

#Expression
  • An expression is a piece of code that produces a value

#Statement
  • is an entire line of code that performs some sort of action

#Augmented Assignment Operator
  • allows the user to use two operators as one to produce an outcome.

For example:

some_value = 5

some_value = (some_value + 2)

Instead of the former, we could do some_value += 2. The += is an example of an Augmented Assignment Operator.

other examples are -= and /=

#String Concatenation

You can add more than one string. for example: print("Hello" + " " + "Daniel")

#Type Conversion

You can convert data types from one type to another.

For example, if you assign the datatype str to 100 as such: str(100) then did print(str(100)), you will not get an error.

Python now identifies (100) as str, and if you print(type(str(100))), Python will tell you the data type, which is str.

#Escape sequences

\ = tells python anything after the \ is a str.

\n = tells Python anything after the \n is a string and to start a new line.

\t = tells Python anything after the \t is a string and to tab the line.

#Formatted strings

If you want a more formatted string, you can use (f).

For example, print("hi" + name + ". You are " + str(age) + " years old.") However, with the format indicator, you can make this much cleaner.

print(f"hi {name}. You are {age} years old"). This will print the same thing but is now cleaner and more dynamic.

#Immutability

Strings in Python are immutable; they cannot be changed.

For Example:

number_set = "01234567"

number_set[0] = "4"

print(number_set)

You will get an assignment error because you are trying to reassign the index of [0] with a new value.

This cannot be done because Python strings are immutable.

#Built-in Functions

https://docs.python.org/3/library/functions.html