Questions, Installation troubleshooting, and command-line
Python Basics
Questions
You will learn:
Basic object types:
Using some built-in fuctions:
print()type()isinstance()int()float()complex()bool()str()= symbol represents the assignment operator. name = "Salem"
print("Name:", name)
print("-----")
age = 20
print("Age:", age)
print("Python is the greatest programming language!")
my_name = "John Johnny"
print(my_name)
my_age = 100
my_age
my_number_list = [1,2,3]
my_number_list
.. Both 3varibale and this.varibale are INVALID.for, while, break, pass, continue, try, exceptin, not, is, import, as, with, if, from, else def, class, return, yield, raises, elif, finallyasdas12for123None12_asdnum_pyimport re
def check(name):
python_reserved_word = ["for", "while", "break", "pass", "continue", "try", "except", "in", "not", "is", "import", "as",
"with", "if", "from", "else", "def", "class", "return", "yiled", "raises", "elif", "finally", "None"]
if name in python_reserved_word:
return "Invalid"
if not isinstance(name, str):
return "Invalid"
if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', name):
return "Invalid"
return f"`{name}` is a valid varibale name."
print(check("asd"))
print(check("as12"))
print(check("for"))
print(check("123"))
print(check("None"))
print(check("12_asd"))
print(check("course"))
Types:
Integers
Floats
Complex numbers
# an integer assigned to a varibale called int_number
int_number = 5
# print the value stored in the variable called int_number
print(int_number)
# what is the type of the value stored in the variable called int_number
print(type(int_number))
my_phone_number = 726353536
type(my_phone_number)
my_phone_number2 = "726353536"
type(my_phone_number2)
converted = int(my_phone_number2)
type(converted)
str(123)
int(123)
my_name = "123"
print(type(my_name))
my_int_name = int(my_name)
print(my_int_name)
int is used to identify the type of the integer and convert objects to integer.# 1) identify the type of the value stored in the variable called int_number
# (we ask here if the type of value is int or not)
print(isinstance(int_number, int))
# 2) convert "10" to integer
example = "10"
print(type("10"))
print(int(example))
# float number assigned to a varibale called float_number
float_number = 0.5983
# print the value stored in the variable called float_number
print(float_number)
# what is the type of the value stored in the variable called float_number
print(type(float_number))
float is used used to identify the type of the float and convert objects to float.# 1) identify the type of the value stored in the variable called float_number
# (we ask here if the type of value is float or not)
print(isinstance(int_number, float))
# 2) convert "10" to integer
example = 9
print(type(example))
print(float(example))
n = 1
type(n)
# can we make n a float?
float(n)
Please go to the following link: https://docs.python.org/3/tutorial/floatingpoint.html
1/3 == 0.333
# integer assigned to a varibale called complex_number
complex_number = complex(5)
print("Complex:", complex_number)
print("Type:", type(complex_number))
print("Type Verfied:", isinstance(complex_number, complex))
print()
print(isinstance(55, int))
print(isinstance("55", str))
| Operator | Meaning |
|---|---|
| + | addition |
| - | subtraction |
| / | division |
| * | multiplication |
| ** | exponentiation |
| // | floor division |
| % | modulus |
# addition
3 + 0.4
# exponentiation
2**2
# floor division -> the result rounds to the nearst integer
21//2
# modulus -> the result is the remainder of a division
a = 7
b = 2
a % b
num_1 = 24
num_2 = 50
my_name = "John"
print(type(my_name))
print(type(num_1))
print(type(num_2))
num_1 + num_2
num_1 / my_name
num_1**num_2
| Operator | Meaning |
|---|---|
| == | equal |
| != | not equal |
| > | greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
# Is 5 > 6
5 > 6
5 < 6
# the two boolean-objects: `True` and `False`
type(True)
# `False` is a boolean-type object
isinstance(False, bool)
isinstance(True, bool)
'hat' != 123
#hat = 2
hat != 123
"hat" == "123"
x = "hat"
x != "123"
| Operator | Meaning |
|---|---|
| and | returns True if both are True |
| or | returns True if one is True |
| not | reverse the output |
True & False
# demonstrating boolean-logic operators
# and
print(True and False)
# another example
num1 = 3
print(num1 > 1 and num1 < 2)
num_1 = 3
num_1 > 1
num_1 < 2
# or
print(True or False)
# another example
num2 = 3
print(num2 > 1 or num2 < 2)
# not
print(not False)
# another example
num3 = 3
print(not(num3 > 1 or num3 < 2))
# example
print(not(num3 == 4))
None is used a lot as a placeholder.type(None)
v = 5
v is None
v is not None
my_name = None