Week One: The Basics of Python, Data Types, Variables, Numbers
Data Types and Variables in Python:
Variables:
Variables are like labeled boxes to store different types of data in them. You can call the data by simply calling the label of that box, for example:
my_name = "Fares"
age = 19
In the example, we created two boxes labeled my_name and age, each one of them stores a value (Fares, 19). I can use them in my program by calling the variable of that value; for example, using the print() function (we will talk more about functions later), we can print the data assigned to those variables
my_name = "Fares"
age = 19
print(my_name) # Output: Fares
print(age) # Output: 19
Rules of Naming a Variable:
- Only start with a letter or underscore ( _ )
- only contain alphanumeric characters and underscores
- variables names are case sensitive; 'age' and 'Age' are different
- cannot be one of Python's reserved keywords, e.g., 'if,' 'class,' or 'def' Tip: Naming conventions to follow :
- Use lowercase, with separate words separated by an underscore example : my_variable_name = 'Fares is the best programmer.'
- Use descriptive names to better communicate with team members or your future self; use user_age to name a variable instead of 'age' or 'ua.'
- Avoid using single-letter variable names. ## Data Types in Python:
-
Strings: A sequence of characters enclosed in single or double quotation marks, like
'Hello world!' -
Integer: A whole number without decimals, for example,
10or-5. -
Float: A number with a decimal point, like
4.41or-0.4. -
Boolean: A true or false type, written as
TrueorFalse. -
Set: An unordered collection of unique elements, like
{4, 2, 0}. -
Dictionary: A collection of key-value pairs enclosed in curly braces, like
{'name': 'John Doe', 'age': 28}. -
Tuple: An immutable ordered collection, enclosed in brackets, like
(7, 8, 4). -
Range: A sequence of numbers, often used in loops, for example,
range(5). - List: An ordered collection of elements that supports different data types.
- None: A special value that represents the absence of a value.
To get the data type of a variable, you can use the
type()function:immutable: Immutable means it cannot be changed after it is created.
-
Function: A function is a named block of code that runs when called.
It takes inputs, does work, and can return a result.
Strings:
A string is a mix of characters written between two double quotation marks or two single quotation mark.
In some languages'hello fares'and"hello fares"are treated differently, but not in python
If you have a multiple-line string, you can use three double or single quotation marks.
e.g :
long_str = """ multiple
line
string """
long_str_2 = ''' another
long
string '''
If your string contains either single or double quotation marks, then you have two options:
- Use the opposite kind of quotes. That is, if your string contains single quotes, use double quotes to wrap the string, and vice versa:
msg = "It's a sunny day"
quote = 'She said, "Hello World!"'
- Escape the single or double quotation mark in the string with a backslash (
\). With this method, you can use either single or double quotation marks to wrap the string itself:
msg = 'It\'s a sunny day'
quote = "She said, \"Hello!\""
sometimes if you have a really long string and you wander if their is a specific character or a word in that string, python have this really cool operator called in to find a character or a word in the string, e.g :
# write a very long paragraph
me = """ this is a very long paragraph to explain the way to write a long string and search for a specific character or a word by using the operator 'in' """
# see if that string have 'python' in it
print ('python' in me) # True
print ('fares' in me) # False
- There is different types of operations to do in string, we will talk about those more deeply in the future.
Numeric Data
integers:
integers are whole numbers, no decimal points, and can be positive or negative.
a = 56
b = -4
type(a) # int
type(b) # int
- we used the
type()function we talked about before. ### Operations to do on integers #### Addition+Adds two integers.
a = 56
b = 12
result = a + b # 68
Subtraction -
Subtracts one integer from another.
a = 56
b = 12
result = a - b # 44
Multiplication *
Multiplies integers.
a = 12
b = 4
result = a * b # 48
Division /
Divides integers.
a = 56
b = 12
result = a / b # 4.666666666666667
Important
- Dividing integers with
/always returns a float -
int / int → float## Floats: - Floats are numbers that contain a decimal point
- They can be positive or negative
Examples:
3.14, -0.5, 0.0
x = 4.9
y = -12.0
type(x) # float
type(y) # float
Basic Operations with Floats
Addition (+)
5.4 + 12.0 # 17.4
Subtraction (-)
12.0 - 5.4 # 6.6
Multiplication (*)
12.0 * 5.4 # 64.80000000000001
- Long decimal values happen because floats are stored approximately in memory
Division (/)
12.0 / 5.4 # 2.222222222222222
Mixing int and float
- When an int and a float are used together, Python converts the result to a float
56 + 5.4 # 61.4
type(56 + 5.4) # float
-
This applies to:
- Addition
- Subtraction
- Multiplication
- Division
-
Rule:
If a float is involved, the result is a float.
How Does the Print Function Work?
every language has it's own way to print output into the terminal, in python we use the function print().
to print the sentence "Hello, fares" we put "Hello, fares" inside the print function, we use (" ") to tell python that this is a string.
a string is a sequence of characters surrounded by either single (') or double (") quotation marks.
In thepython print('Hello fares!')example, the string'Hello fares!'is an argument passed to theprintfunction. You can also use theprintfunction to show multiple values, or arguments, at once by separating them with commas.
Python automatically adds a space between each item when you separate them with commas.
Beginner Practice Task: Data Types, Variables, Strings, Numbers
Goal
Practice creating variables, using basic data types, and combining strings with numbers.
Rules
Use only:
- variables
- strings
- integers and floats
- basic arithmetic (
+,-,*,/) print()
Task Description
- Create the following variables:
-
name→ a string with your name -
age→ an integer -
height→ a float (meters) -
favorite_number→ an integer
-
- Create new variables using the previous ones:
-
age_next_year→ age + 1 -
height_cm→ height multiplied by 100
-
- Print the following sentences exactly using variables
My name is <name>I am <age> years oldNext year I will be <age_next_year>My height is <height_cm> cmMy favorite number doubled is <favorite_number * 2>
Expected Practice Focus
- Knowing when to use
int,float, andstr - Assigning values to variables
- Performing simple calculations
Top comments (0)