3  Basic Syntax

3.1 Basic types

values = {
    "text": "hello",
    "integer": 10,
    "decimal": 3.14,
    "boolean": True,
    "nothing": None,
}

for name, value in values.items():
    print(name, type(value).__name__)
text str
integer int
decimal float
boolean bool
nothing NoneType

4 Str string

print(type('piece of text'))

some_text = "other way to assign string to a variable"

long_str = ''' bla bla bla 
more bla bla 
and finally end '''
<class 'str'>

5 Operations with string

name = 'Isaiah'
last_name = 'Moraki'
name + ' ' +last_name 
'Isaiah Moraki'

6 Concatenate

'texto 1' + 'texto 2'
'texto 1texto 2'

7 Type conversion

a = 100
b = str(a)

print(a)
print(b)

type(b)
100
100
str

7.1 Escape sequence

#weather = It's sunny' # Error

weather = "It's sunny"

weather = "It\'s \"kind of\" sunny" # You can use backslash
print(weather)


weather = "\t It\'s \"kind of\" sunny" # \t add a tab space
print(weather)

weather = "\n It\'s  \n\"kind of\" sunny" # \n add new line
print(weather)
It's "kind of" sunny
     It's "kind of" sunny

 It's  
"kind of" sunny

7.1.1 Formatted strings

age = 46
name = 'Julius'

print(f'My age is {age} years old, and my name is {name}'  )

### Immutability: important term in programming, string are immutable en py. You cannot chane the value once it's created

last_n = 'Monroy'
# last_n[start:stop:stepover]

last_n[2:3:]
My age is 46 years old, and my name is Julius
'n'

7.2 Méthods

quote = "I'm so higher than ever"

quote.lower()

quote.upper()

quote.lower()
"i'm so higher than ever"

8 Augmented assigment operator

value = 5

value += 3 # This operator increases by two units 

print(value)

value = 5

value -= 2 # Also we can use it fo substraction

print(value)
8
3

8.1 Things to remember

  • Python is case-sensitive.
  • Indentation is part of the syntax.
  • Use meaningful variable names.
  • = assigns a value; == compares values.