Anna GE 8151 - Variables in Python
School name Anna University
Pages 2

Unformatted text preview:

What is a Variable A variable is a container where you can store values For example we can store 2 in a variable called x Variables are useful because they allow us to change the value of the container and we can use them in operations For instance if we have x 2 and y 3 we can add them together x y 5 Variables can store different types of values such as integers and strings We don t need to define the type of the variable since it automatically takes the value of whatever we assign to it Using Variables in Operations We can use variables in operations just like we do with numbers For example we can change the value of x from 2 to 9 We can also use underscores to represent the output of the previous operation For example if we add x 10 we can use the output of that operation 19 in another operation 19 y 22 We can also fetch individual characters from a string stored in a variable using square brackets The numbering of characters starts at 0 For example the first character of youtube is y and the last character is e We can also use negative numbers to start counting from the end of the string Working with Strings in Python Let s start by looking at how to access specific characters in a string using index values To access the first character we use index 0 To access the second character we use index 1 and so on We can also use a colon to access a range of characters For example 0 2 will give us the first two characters It s important to note that the ending value in a range is exclusive meaning it will not be included in the result If we want to access the first three characters we would use 0 3 If we don t specify an ending value the range will go until the end of the string If we specify an ending value but not a starting value the range will start at the beginning of the string If we try to access characters outside the range of the string we will get an error While strings in Python are immutable meaning we cannot change individual characters we can concatenate strings and print them out in different ways We can also use the built in len function to find the length of a string

View Full Document

# Anna GE 8151 - Variables in Python

Pages: 2
Documents in this Course

177 pages

2 pages

20 pages

3 pages

4 pages

7 pages

7 pages

53 pages

26 pages

3 pages

3 pages

2 pages

70 pages