Python Beginner Tutorial #2 - Variables and Data Types

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

Explains Python variables and data types including typecasting

Full Transcript

what's going on guys welcome to the second episode of the Python tutorial series for beginners in today's video we're going to talk about variables and data types now in the last episode we wrote our first hello world program and mentioned that we need to use quotation marks to define the data type of the value now all values in pythons have data types in this case it was a text value and the data type which is used for text and Python is called string it is one of the multiple data types we can choose from and it is called string because a text is actually just a string of characters so in this video we're going to get a brief overview of the different data types that Python offers and we're also going to learn how to work with variables now let us start with the data type that we already know the string the string has the keyword STR and we're going to talk about the importance of these keywords later on in this video but for now just know this string has the keyword STR and it always has to be surrounded by some kind of quotation mark so we can use double quotation marks we can use single quotation marks and we can also use a triple quotation marks if you wanted to find multi-line strings so this would be a multi-line string over multiple lines and then we closed it again with a triple quotation so this is basically our text data type next data type on our list is the integer and an integer represents a whole number so everything like 10 or 9 or also minus 3 everything that has no decimal places that's just a whole number either negative or positive is an integer so the basic number type you could say if for some reason however you need to work with decimal places you can work with floating-point numbers these have the keyword float and these are numbers like ten point nine or nine point three three four five or minus eight point seven six three whatever these are floating-point numbers and we use them if we need more precision in our calculations now with that's still not enough and you're working with some advanced mathematics Python also allows you to define a complex number so for this you use the complex function and you pass a real part like 10 for example and an imaginary part let's say 5 and this would give you a complex number that you can do calculations with so 10.5 I in this case now if you don't know how complex numbers work it doesn't matter because that's more into mathematics it's not so much programming so if you don't need it just don't use it so these are the number types but there's also a special datatype called boolean and the boolean has the keyword bool and it can only have one of two values so it can either be true with a capital T or Falls with a capital F and it's basically a binary type because it cannot be something in between it has to be either 1 or 0 on or off or true or false and so on we're going to use this datatype a lot when it comes to conditions if statements and loops and future episodes so these are the basic datatypes actually we also have some sequences like lists and tuples and dictionaries and so on but these are sequences and they will be the topic of future episodes so we're going to skip them for now so let us now talk a little bit about the importance of data types for example when we have the string 10 and the number 10 or the integer 10 these would give us the same output when we print them but they are very different and they behave differently as well when we print the result off the strings temples 10 for example let's say 10 plus 10 we would get a different result then we would get when we print the integers 10 plus 10 so let us form the script as you can see when we say 10 plus 10 in the string world it says append 1 string onto the end of another string because we have the string 10 and when we add another string 10 it just puts another string at the end of the first string however if we add two numbers of course we get the sum the arithmetic sum the result of the calculation and this somehow gets us to the next question how can we know what data type a value has will not always be able to determine it by just looking at it because sometimes the values will be stored in variables but for this we have a specific function called type the type function returns the data type of a variable or a value so let's say type 10 and this will return me the data type of this value returning means that it just gives me this data type does not mean that it prints the data type but just I get this value back so I could save it in a variable later on or I can print it so what I do here is I say give me the type of 10 and then print this result so in this case we would get class int for integer and when announced quotation marks so it's now a string when I run this I get class STR for string so if we're not sure what datatype we're dealing with we can always use the type function now let us get to the second part of this video the variables defining variables in Python is very simple we just choose a variable name like X or Y or something else and we then assign a value to it so ax could be 10 Y could be hello and whatever could be true now our variables work like placeholders so we can use them for further processing this is useful because sometimes we don't know what the value of the variable will be but we still want to perform operations on it this might be the case with user input or when we load data from the internet so I could just go ahead and say print X for example and it would out print the letter X it would print the value that's stored in X so 10 and if I print wide would give me hello and so on or if I say Y is 20 for example I can say print X plus y save it and I would get 30 so I can use variables as placeholders now python is a dynamically typed language this means that we don't have to define a specific data type for our variable in advance also our variable can change its data type all the time in other language like Java or C++ we would have to define a static data type for each variable so it would still be possible to change the value but an integer variable could not just change the value to a string in Python this is possible so we can say x equals 10 and it starts as an integer later on I can say x equals some string and then I can say x equals true and it changes not only its value but also its data type in other programming languages or in most programming languages this is not possible I would have to say something like int X and then I would have to stay in the integer realm I would have to stay with this datatype so we're now getting to the last part of this video which is about typecasting now imagine we have some user input that is of the data type string but it actually contains a number and we want to do calculations with it so for example I could say user input X equals number 120 but in a string format in this case we couldn't do any calculations with the input because when we try to divide it by and for for example let's say print X divided by 4 what I would get is a type error so as you can see type error because I cannot just use this operator for division on strings and integers so I have to have two integers or two floats two number types basically so we would have to convert the value to another data type and we do this by using the data type keywords that we mentioned in the beginning of this video so when I have the variable X and I want to get an integer out of it what I can do is I can say x equals and then I use the int function so I use the int keyword for integer and I pass to it the value of x so I have x equals into X so I get the value of X turn it into an integer and then assign it back to X now that we have done this we can do the calculation with it because you can see 120 over 4 equals 30 and it works because now we're dealing with two number types however this only works when the string really contains a number otherwise we would get a so called value error because we cannot just typecast a text into an integer so if I have x equals hello I cannot just go ahead and say typecast into int because as you can see value error I cannot just convert some letters into numbers so that's everything you need to know about variables and data types for now in the next video we're going to cover operators and user input so these will allow us to perform actions on our values and variables so keep going and watch the future episodes it will get more and more interesting over time and if you haven't done it yet subscribe to this channel if you want to see more hit the like button if you enjoyed this video and feel free to ask questions and give feedback in the comment section down below thank you very much for watching and see you in the next video bye [Music] you

Original Description

In today's episode we are talking about variables and data types. At the end, we also mention typecasting and how to convert from one data type into another. Website: https://www.neuralnine.com/ Instagram: https://www.instagram.com/neuralnine Twitter: https://twitter.com/neuralnine GitHub: https://github.com/NeuralNine Programming Books: https://www.neuralnine.com/books/ Outro Music From: https://www.bensound.com/ Subscribe and Like for more free content!
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 3 of 60

1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

Related Reads

Up next
AI Vlogs FREE* 🔥 Make AI Vlogging videos with AI influencers #ai #aivlog #aivideo
Raj Photo Editing and Much More
Watch →