DEV Community

lordronjuyal
lordronjuyal

Posted on

day 05

date: 24 May, 2024.

False values:- When we use bool() function to convert other data types, it does the conversion on the basis of these rules:

  1. Number 0 and all empty data structures like empty string "", will turn into False
  2. Numbers other than 0 and non-empty data structures will turn into True

Logical operator: There are 3 operators we use in Python, for combining different values.

  1. and -- This returns the first false value it encounters, if not it will return the last value.
    eg a = True and 23 and "" and 0 and 15

    a = ""
    a = True and 23
    a = 23
    If we use this in conditional statements, these will auto-convert using the bool function in the background and we will get a boolean value.

  2. or -- This returns the first True value it encounters, if not it will return the last value.
    eg a = True or 23 and "" or 0 or 15

    a = True
    a = 0 and 23
    a = 0
    Like 'and', it gets converted into boolean values in conditional statements.

  3. not -- Unline 'and' (or) 'or' it will return the boolean values (True or False) opposite the value it's given
    eg not True >>False
    not False >> True
    not 0 >>True
    not "ABC" >>False

Loops:- Another type of control statement is loops, where we can execute a code block multiple times till a condition is satisfied or a timer runs. Iteration is another name for this process.

For loop:- We can iterate over a code in two ways

  1. using list -- for item in list: This will iterate for each item in the list and we can use that item in our code.
  2. using range -- for x in range( a, b, step): The range function will create a range starting from a and ending 1 step before b. step is the gap we wanted to be added to each iteration in a. If step is not passed, it will take it as 1. Finally, if only 1 number is passed, the loop will start from 0 to b-1.

while loop:- Syntax for it- while condition:
It will iterate till the condition is true. We need to have a termination condition or logic in this, or else it will run infinitely and crash the interpreter.

Functions I learned:-

  1. join: this is a method on the list, used to generate a string from items. Syntax is "x".join(list) This will generate a string with x between each item. If we use an empty string(no x) we will get a string with each item together without space.
  2. shuffle: this is a method available on random module. Its syntax is random.shuffle(list) This will randomly shuffle the items in the list, changing their index numbers.

Functions:- We can make our own functions in Python.
The syntax for it is: def name_of_function():
Remember to have an indentation in the code below it so that it is included in the function code block.

Flow chart: This is a chart of steps we want our program to follow. It will tell us how the interpreter will follow logic so that we can write better code.

Program:- 1) Hangman
flow char for hangman

Some points I learned :

  • use _ as a variable name when we are not using that variable, like looping through a list but not using its items.

  • list+= x will do same thing as list.append(x). It will add x as its last item. But be careful as it will separate characters if more than one. Using append is better choice

  • when coding with while always put condition = false at the end, so that it doesn't run infinity by mistake.

  • to check if a value is present in the list or not, we use-- if x in string:
    we can also check it in the list.

  • the import module command should be at top. It won't show any error though if it's not at the top. ( I don't know the reason for it, though)

https://replit.com/@rohitrj332024/Hangman-day-7#main.py


Personal --> Next 3 days I am taking off from the market so, I will try to cover most of the Python topics in the course.
One thing I hate about writing a blog on this site is that the tab key doesn't work here. It irritates a lot. I will probably write my code on Google doc now on. The only thing stopping me is I will first have to find how to put dark mode in that.

Top comments (0)