Python basics simply explained by someone younger than your 90 years old teacher in school
in this book we will take a look on:
- useful shortcuts
- idle python envionment
- basic commands
- Tkinter graphic
- user interface
- bringing our previous p...
There are some more but we wont be using them any soon.
back to variables: as variable we will be using letter x, however if you want you can use others letters or words aswell
1. saving number: - x = 28 - in this scenario we just saved number 28 to variable x x = 1.5 - here we overwrote number 28 to decimal number 1.5, python saved us the struggle with data types. x = "15" - here we saved this number as text, if we now tried to substract something from our variable we will exeperience error.
2. saving text x = "hello world" - now you can try this command: print(x)
3. assign-ing data type to variable x = int(15) - now 15 is integer - number data type
3.making a condition:
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
i tried to zoom this picture multiple times, but wattpad keeps messing with its size
With symbol = we assign data to a variable. With symbols == we compare, if x ==15 - means that if x is equal to 15, our condition will happen. Use this != if you want condition to happen when its not equal! In you can use: IF multiple times, you dont have to use: ELSE at all!!!!!
4. Input It would be quite annoing if user of our program would have to open code and write in it if he wants to change value of some variable, to fix this we can use command input, it will allow user to write value of the variable in shell, instead of opening code.
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
!!! when user tries to input letters program will probbably crash or it will just throw error on your face:) as letters cannot be saved into integer (int)!!! Same will happen with decimal numbers. this command will work just once per run(if you want to input another number you will need to run program again) we will talk about ways how to fix this in capitole that will be called something like: user interface, if it doesnt exist yet, sowwy you came to early.
5. Random
generating random numbers, for this we will need to import module: random. Luckily for you, its already downloaded in idle python.
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
this code will generate random numbare in range 1-20, run it through our condition and than print this number + the text.
6. loop
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
output in shell will look like this:
11 customer is younger than required age 17 customer is older than minimumage 4 customer is younger than required age 11 customer is younger than required age 9 customer is younger than required age 5 customer is younger than required age 20 customer is older than minimumage 3 customer is younger than required age 12 customer is younger than required age
you may have noticed that our loop ran just 9 times, loop went from 1-10
7. while cycle
we use this one when we want to basicaly make a loop that wont stop untill some condition happens.
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
output:
18 customer is older than minimumage19 customer is older than minimumage6 customer is younger than required age20 customer is older than minimumage12 customer is younger than required age4 customer is younger than required age3 customer is younger than required age7 customer is younger than required age5 customer is younger than required age
our code was running untill x was equal to 5 !!!be careful, while cycle is very powerful but dangerous command, when mistake (bug) happens this cycle is can run forever = if you put into cycle some more advanced event, and something goes wrong, it can pretty much kill your pc performance and the app will probbably crash due to this. Example: if we put into our cycle that it will run untill x = 25 it will go forever, this wont be such a problem as there isnt any complicated function in our cycle. We will talk about possible solutions to this later!!!
In next capitole you can find some easy task to practice your new knowledge