Python is a popular programming language with the most simple syntax which enables developers to write smaller lines of code as compared to other programming languages. Python was designed for readability, and has some similarities to the English language with influence from mathematics. It uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Also, it relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes while other programming languages often use curly-brackets for this purpose.
Python syntax
Python syntax can be executed by writing directly in the Command Line or by creating a python file on the server, using the .py file extension which is the run in the command line.
Commenting in python
comments are used to make python code/ program more readable, provide further explanation and also, during testing to prevent execution of certain lines of code. A comment starts with the # symbol.
Creating Variables in python
Variables are containers for storing data values.
Python has no command for declaring a variable instead it is created after assigning values.
x = 10
y = "Fridah"
print(x)
print(y)
Also, you can get the type of the function by :
x = 10
y = "Fridah"
print(type(x))
print(type(y))
It is important to note that variable names are case-sensitive hence varA cannot overwrite varB
To gain better understanding of python it is adviced to try hands on projects and hence lets create a simple guessing game:
`### GET GUESS
import random
def get_guess():
return input("what is your guess")
GENERATE COM CODE 123
def generate_code():
digits = [str(num) for num in range(10)]
random.shuffle(digits)
return digits[:3]
take user guess and code and compare the no in a loop and create a list of clues according to matching parameters
def generate_clues(code,user_guess):
if user_guess == code:
return "code cracked"
clues = []
for ind, num in enumerate(user_guess):
if num == code[ind]:
clues.append("match")
elif num in code:
clues.append("close")
if clues == []:
return ["nope"]
else:
return clues
print('welcome code breaker!')
secret_code = generate_code()
clue_report =[]
while clue_report != "CODE CRACKED!":
guess = get_guess()
clue_report = generate_clues(guess,secret_code)
print("here is the result of your guess:")
for clue in clue_report:
print(clue)
x = get_guess()
print(type(x[0]))`
Top comments (0)