That means that you can restrict the user to type something no longer than 10 characters, or to type only an integer or a float. Here I going to show you those three cases (something no longer than ten characters, an integer, and a float).
So let’s start with the easiest one; to restrict the number of character the user can type, it is easy to use the length command.
len(variable)
it will return the number of characters in case of a string, the number of elements it has in case of a list.
REMEMBER, in order to do this, the variablke must be either a string or a list, otherwise it won’t work
With a loop with the condition of what the user is typing is longer than ten characters, you can r4estrict the interaction of the user and don’t let him go until he types what you want
So for the program we will ask a nickname no longer than ten characters, the loop after asking the nickname will be:
So it will loop untill the user types it correctly
Then, we will review a little harder one; to restrict the user to only type an integer; that means the input won’t have a “.” inside of it, and it can be converted to integer via python.
There exists a common way to validate that which is by exceptions in a loop. With a loop that it’s only condition is to be true, you will use the command try, to, well, try to do something, in this case, to convert the input of the user to integer via python, variable = int(variable); if it results like an error, represented in python as ValueError, then it will ask again for the number. This loop is to make sure the user didn’t input letters, just numbers.
After doing that, is necessary to verify if the input has a decimal point”.”, in that case, it will ask again for the number
So it will look like this
The last one, the float, its a little more ease, it just have to work if you try to convert it to float; you will use the same structure, but you will try to convert it to float instead to int variable = float(variable)
It will result like this:
Here you saw not just how to validate, but learned the basic types of data
The strings, that are just characters
The integers, that are just numbers, not fractions, not decimals
The floats, that are numbers with decimal points
It is easy isn’t it?
]]>Here is the link to my video:
]]>
The Zen of Python is a collection of 19 software principles that influences the design of Python Programming Language, written in August 2004 by Tim Peters. The principle text is released into public domain.
Zen of Python is written as an informational entry number 20 in Python Enhancement Proposals (PEP), and can be found on the official Python website. It is also included as an Easter Egg in Python interpreter, which would be displayed by entering a statement import this
Mastery 9 – Basic types and their use in Python
Python has 6 different basic types of data:
Integers
These are numbers that belong to all real numbers
Floating point numbers
Real numbers with its decimals
Strings
These are any kind of characters, letters, numbers, etc. They are in between quotation marks (” “)
Tuples
This is an ordered set of values with fixed number of elements, goes in parentheses.
Lists
A set of values ordered without fixed number of elements, they are in square brackets
Dictionaries
Elements used to map keys to their assigned values. Their keys and values can be of any type
]]>In Python 3 we have:
INTEGERS
We can say an ‘integer’ is an integer, some examples of this could be:
1, 23, 3909, -23, 4, -189
Unlike other programming languages python use Integers not matter how high or small it is as long as it is integer the type of the number will be INTEGER
With INTEGERS we can do many things, the basic could be math like:
>>>3+9
12
>>> 5-6
-1
But also INTEGERS can be used in the rest of the program, they are a essential part of Python, you have to be very attentive in do not confuse with string because 43 != ’43’.
Example of assign a INTEGER to a variable and assign a INTEGER of an input.
>>>num_Int = 3
#The first number is a variable that has the value of 3
>>>input_Int= int(input(‘Give me a number’))
#The second ask for the user to put a number and covert it to INTEGER
FLOAT
Float numbers are similar to integer just because they’re numbers too, but FLOAT allow you to use decimal numbers and this open a new world, because just imagine try to work with numbers like π or ‘e’ and use INTEGERS… it won’t work right?
As equal as INTEGER, FLOAT numbers can be used in mathematical operations.
>>>1.2+5.1
6.3
Also the float numbers can be also be assign to a variable or an input.
>>>num_Float = 3
#The first number is a variable that has the value of 3
>>>input_Float= int(input(‘Give me a number’))
#The second ask for the user to put a number and covert it to FLOAT
STRINGS
Strings are a very special type in Python because if you are familiar with other programming languages maybe you know that strings are not objects, but here in Python THEY ARE!
So you can use a string like a object and make a lot of things with it.
>example_string = ‘Hi everyone!’
>>>example_string
Hi everyone!
>>>len(example_string) #Length of the string
5
>>>type(example_string) #Type of the class
<class ‘str’>
Python String Official Reference
TUPLES
Tuples are a special type of ordered list in Python 3, because with Tuples you can make a list but you can’t modify it, it remains always the same.
To make a tuple the process is the following (very simple):
>>>example_Tuple = 1,2,3
>>>type(example_Tuple)
<class ‘tuple’>
To create a tuple just type the elements without any bracket or keys!
But why use tuples if they can’t be edited?
Well sometimes even if you don’t believe me Tuples are necessary just imagine for example that you are in charge of many schools of a region and you really don’t want to miss information, does a tuple can be useful?…
Of course because this way you secure the fact that any school name will be missed.
Python Tuples Official Reference
LISTS
List are amazing, as its name says it is a ordered list of things like Tuples but LIST CAN BE EDITED.
To make a list is very simple:
>>example_List = [1, 2, 3]
>>>type(example_List)
<class ‘list’>
#in Lists you can add [use list.append(x)]
>>>example_List.append(4)
>>>example_List
[1, 2, 3, 4]#in Lists you can remove [use list.remove(x) or list.pop(x)]
>>>example_List.remove(3)
>>>example_List
[1, 2, 4]#In Lists you can move elements inside and outside [use list.insert(x,y)]
>>>example_List.insert(2,3) #In second space insert number 3
>>>example_List
[1, 2, 3, 4]
Lists are very useful when you know how to use it, because it can help you with stacks and also when you are open a file in Python
Python3 Lists Official Reference
DICTIONARIES
Dictionaries are a link between anything and anything, they are very useful because it keep order the information, and provides different uses.
To make a dictionary follow the following example:
>>family_Age = {‘Me’:18, ‘Sister’:24, ‘Mom’:50, ‘Dad’:55}
>>>type(family_Age)
<class ‘dict’>#If you call a specific element it return the value
>>>family_Age[‘Mom’]50#You can add elements very simple
>>>family_Age[‘Uncle’]=47
>>>family_Age
{‘Me’:18, ‘Sister’:24, ‘Mom’:50, ‘Dad’:55, ‘Uncle’:47}#Also delete
>>>del family_Age[‘Me’]
>>>family_Age
{‘Sister’:24, ‘Mom’:50, ‘Dad’:55, ‘Uncle’:47}
Dictionaries are very useful for several things, if you want to know more about click the following link.
]]>So, to start we need to learn that Python works with values but there are differents types of value like:
Strings: because it contains a “strings” of letters. It basically it takes everything literal it doesnt matter if you write a number if it has ” .. ” the program wil consider it as a string.
Integrers: are basically numerical values, like 2,4,5,3,6, etc. but whole and can go from negative to positive it doesnt matter.
Float: they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Like; 3.2, 5.4, 7.87, etc.
Tuples: A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples
are immutable.
Syntactically, a tuple is a comma-separated list of values:
>>> t = ‘a’, ‘b’, ‘c’, ‘d’, ‘e’
Although it is not necessary, it is common to enclose tuples in parentheses:
>>> t = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’)
To create a tuple with a single element, you have to include a final comma:
>>> t1 = ‘a’,
>>> type(t1)
<type ‘tuple’>
A value in parentheses is not a tuple because it will get confused with a string (thats why is marked like type str):
>>> t2 = (‘a’)
>>> type(t2)
<type ‘str’>
List: A list is a sequence of values. In a list, the values can be any type. The values in a list are called elements or sometimes items. There are several ways to create a new list; the simplest is to enclose the elements in square brackets ([ and ]).
Dictionaries: A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type.
Boolean: A boolean expression is an expression that is either true or false. True and False are special values that belong to the type bool; they are not strings.
]]>There are several types in C++ but the basic ones are char, int, long, float, and double.
Each one serves a specific purpose when a variable is used.
char type consists of characters used in a function. It is used when your output will consist of a character or characters. For example, we used it on mastery 19 when our output was a character.
int type is the most used and common in C++. We can use it for numbers but not so big ones. We used it on mastery 10 where our output was a simple math operation.
long type is similar to int but allows us to have bigger integer number outputs. We used it on mastery 21 where our factorial numbers could get bigger.
float and double allows us to have non integer number outputs. For example, the division of 3/2 = 1.5, float allows us to have that output and double works as long, where we can have bigger non-integer number outputs. float was used on wsq10 where a divition could be a non-integer number.
Hope I could clear some doubts and hopefully you know now when to use each type!
]]>There are several types in C++ but the basic ones are char, int, long, float, and double.
Each one serves a specific purpose when a variable is used.
char type consists of characters used in a function. It is used when your output will consist of a character or characters. For example, we used it on mastery 19 when our output was a character.
int type is the most used and common in C++. We can use it for numbers but not so big ones. We used it on mastery 10 where our output was a simple math operation.
long type is similar to int but allows us to have bigger integer number outputs. We used it on mastery 21 where our factorial numbers could get bigger.
float and double allows us to have non integer number outputs. For example, the division of 3/2 = 1.5, float allows us to have that output and double works as long, where we can have bigger non-integer number outputs. float was used on wsq10 where a divition could be a non-integer number.
Hope I could clear some doubts and hopefully you know now when to use each type!
]]>Here is the link
]]>
Here is the link
]]>
Basic output (print) in Python