In another blog post from my learning progress we will cover Variables and Strings in Python.
What are variables ?
From my understanding variable is something that holds a value assigned to it.
In Python you can name your variables whatever you want but with some restrictions.
- Variables must start with letter or underscore
# correct
_name
age
#wrong
2names
- The rest of the name must consist of letters, numbers, or underscores
# correct
cats2
#wrong
hey@hey
- Names are case sensitive
CATS != Cats
Cats != cats
Naming conventions
- snake_case (underscore between words)
first_name
- lowercase, with some exceptions
CAPITAL_SNAKE_CASE usually reffers to constants eg.
PI = 3.14
UpperCamelCase usually reffers to a class
Variables that start and end with two underscores (called "dunder" for double underscore) are supposed to be private or left alone
__leave_me_alone__
Dynamic typing
Python is highly flexible about reasigning to different types
Example:
awesomeness = True
print(awesomeness) # True
awesomeness = "a dog"
print(awesomeness) # a dog
In this example you can see in 1 line that we assigned boolean value to awesomeness variable and later on line 4 we assigned a string value of "a dog" to awesomeness variable
In C++ this will result in error as C++ is Static Typed that means it has to stay with type that was original assigned to it
# integer
int not_awesomeness = 3;
# string
awesomeness = "cool" # !Error
Special value None
Value "None" represents nothing. If you're experienced with other programming languages such as Javascript or PHP the Python value "None" is equals to the "null" value in JS or PHP.
String Concatenation
In Python we can connect or join strings together (as many as you want).
We would like to do it with "+" a plus sign.
Example:
first_name = "Branislav"
last_name = "Buna"
print(first_name + last_name) # BranislavBuna
# in case we would like to add space between those strings
print(first_name + " " + last_name) # Branislav Buna
# or include something else than space
print("First name: "first_name + " and " + "Last name: "+ last_name)
# First name: Branislav and Last name: Buna
String formating
There're several methods to format strings in Python to interpolate variables.
- The new way (in Python 3.6+) => F. strings
x = 10
formated = f"I've told you {x} times already"
# I've told you 10 times already
- The tried-and-true way (in Python 2 -> 3.5) => .format method
formated = "I've told you {} times already".format(10)
# I've told you 10 times already
String indexes
Anytime we would like to access the particular character index/position in the given string we might look on this method.
Example:
str = "hello"
str[0] # h
You might me asking why we started with 0 ? Well it is because in programming we call it Zero-based numbering
str = "hello" # 0 = h , 1 = e, l = 2 , l = 3, o = 4
str[0] # h
# we can also ask for character with negative number this will give us ability to go from the back of the string
str[-1] # o
Well that might be it today, I hope you will enjoy.