Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Power Lifting Lever Belt
SkullVamp
Updated:
Yesterday at 10:32 PM
Ad icon
port.lk Domain for sale
Lankan-Tech
Updated:
Yesterday at 3:55 PM
Colombo
Kaduwela - Two Storey House for Sale
dilrasan
Updated:
Thursday at 2:23 PM
Ad icon
Wechat qr verification
Pawan2005
Updated:
Thursday at 1:28 AM
🚀 GOOGLE AI PRO 18 MONTHS ACTIVATION 🚀
sayuru bandara
Updated:
Wednesday at 5:34 PM
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Python The Worlds Most flexible and easist Language..
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="gayan kalhara" data-source="post: 3315052" data-attributes="member: 60168"><p><strong>Part 5 Who Goes There?</strong></p><p></p><p style="text-align: center"><strong>3.1 Input and Variables</strong></p> <p style="text-align: center"></p><p></p><p>Now I feel it is time for a really complicated program. Here it is:</p><p></p><p>print "Halt!"</p><p>s = raw_input("Who Goes there? ")</p><p>print "You may pass,", s</p><p></p><p>When I ran it here is what my screen showed:</p><p></p><p>Halt!</p><p>Who Goes there? Josh</p><p>You may pass, Josh</p><p></p><p>Of course when you run the program your screen will look different because of the raw_input statement. When you ran the program you probably noticed (you did run the program, right?) how you had to type in your name and then press Enter. Then the program printed out some more text and also your name. This is an example of input. The program reaches a certain point and then waits for the user to input some data that the program can use later. Of course, getting information from the user would be useless if we didn’t have anywhere to put that information and</p><p>this is where variables come in. In the previous program s is a variable. Variables are like a box that can store somepiece of data. Here is a program to show examples of variables:</p><p></p><p>a = 123.4</p><p>b23 = ’Spam’</p><p>first_name = "Bill"</p><p>b = 432</p><p>c = a + b</p><p>print "a + b is", c</p><p>print "first_name is", first_name</p><p>print "Sorted Parts, After Midnight or",b23</p><p>And here is the output:</p><p>a + b is 555.4</p><p>first_name is Bill</p><p>Sorted Parts, After Midnight or Spam</p><p></p><p></p><p>Variables store data. The variables in the above program are a, b23, first_name, b, and c. The two basic types</p><p>are strings and numbers. Strings are a sequence of letters, numbers and other characters. In this example b23 and</p><p>first_name are variables that are storing strings. Spam, Bill, a + b is, and first_name is are the strings</p><p>in this program. The characters are surrounded by " or ’. The other type of variables are numbers.</p><p>Okay, so we have these boxes called variables and also data that can go into the variable. The computer will see a line</p><p>like first_name = "Bill" and it reads it as Put the string Bill into the box (or variable) first_name. Later</p><p>on it sees the statement c = a + b and it reads it as Put a + b or 123.4 + 432 or 555.4 into c.</p><p></p><p>Here is another example of variable usage:</p><p></p><p>a = 1</p><p>print a</p><p>a = a + 1</p><p>print a</p><p>a = a * 2</p><p>print a</p><p>And of course here is the output:</p><p></p><p>1</p><p>2</p><p>4</p><p></p><p>Even if it is the same variable on both sides the computer still reads it as: First find out the data to store and than find out where the data goes.</p><p></p><p>One more program before I end this chapter:</p><p></p><p>num = input("Type in a Number: ")</p><p>str = raw_input("Type in a String: ")</p><p>print "num =", num</p><p>print "num is a ",type(num)</p><p>print "num * 2 =",num*2</p><p>print "str =", str</p><p>print "str is a ",type(str)</p><p>print "str * 2 =",str*2</p><p>The output I got was:</p><p>Type in a Number: 12.34</p><p>Type in a String: Hello</p><p>num = 12.34</p><p>num is a <type ’float’></p><p>num * 2 = 24.68</p><p>str = Hello</p><p>str is a <type ’string’></p><p>str * 2 = HelloHello</p><p></p><p>Notice that num was gotten with input while str was gotten with raw_input. raw_input returns a string while input returns a number. When you want the user to type in a number use input but if you want the user to type in a string use raw_input. The second half of the program uses type which tells what a variable is. Numbers are of type int or float (which 10 Chapter 3. Who Goes There?</p><p>are short for ’integer’ and ’floating point’ respectively). Strings are of type string. Integers and floats can be worked on by mathematical functions, strings cannot. Notice how when python multiples a number by a integer the expected thing happens. However when a string is multiplied by a integer the string has that many copies of it added i.e. str * 2 = HelloHello.</p></blockquote><p></p>
[QUOTE="gayan kalhara, post: 3315052, member: 60168"] [b]Part 5 Who Goes There?[/b] [CENTER][B]3.1 Input and Variables[/B] [/CENTER] Now I feel it is time for a really complicated program. Here it is: print "Halt!" s = raw_input("Who Goes there? ") print "You may pass,", s When I ran it here is what my screen showed: Halt! Who Goes there? Josh You may pass, Josh Of course when you run the program your screen will look different because of the raw_input statement. When you ran the program you probably noticed (you did run the program, right?) how you had to type in your name and then press Enter. Then the program printed out some more text and also your name. This is an example of input. The program reaches a certain point and then waits for the user to input some data that the program can use later. Of course, getting information from the user would be useless if we didn’t have anywhere to put that information and this is where variables come in. In the previous program s is a variable. Variables are like a box that can store somepiece of data. Here is a program to show examples of variables: a = 123.4 b23 = ’Spam’ first_name = "Bill" b = 432 c = a + b print "a + b is", c print "first_name is", first_name print "Sorted Parts, After Midnight or",b23 And here is the output: a + b is 555.4 first_name is Bill Sorted Parts, After Midnight or Spam Variables store data. The variables in the above program are a, b23, first_name, b, and c. The two basic types are strings and numbers. Strings are a sequence of letters, numbers and other characters. In this example b23 and first_name are variables that are storing strings. Spam, Bill, a + b is, and first_name is are the strings in this program. The characters are surrounded by " or ’. The other type of variables are numbers. Okay, so we have these boxes called variables and also data that can go into the variable. The computer will see a line like first_name = "Bill" and it reads it as Put the string Bill into the box (or variable) first_name. Later on it sees the statement c = a + b and it reads it as Put a + b or 123.4 + 432 or 555.4 into c. Here is another example of variable usage: a = 1 print a a = a + 1 print a a = a * 2 print a And of course here is the output: 1 2 4 Even if it is the same variable on both sides the computer still reads it as: First find out the data to store and than find out where the data goes. One more program before I end this chapter: num = input("Type in a Number: ") str = raw_input("Type in a String: ") print "num =", num print "num is a ",type(num) print "num * 2 =",num*2 print "str =", str print "str is a ",type(str) print "str * 2 =",str*2 The output I got was: Type in a Number: 12.34 Type in a String: Hello num = 12.34 num is a <type ’float’> num * 2 = 24.68 str = Hello str is a <type ’string’> str * 2 = HelloHello Notice that num was gotten with input while str was gotten with raw_input. raw_input returns a string while input returns a number. When you want the user to type in a number use input but if you want the user to type in a string use raw_input. The second half of the program uses type which tells what a variable is. Numbers are of type int or float (which 10 Chapter 3. Who Goes There? are short for ’integer’ and ’floating point’ respectively). Strings are of type string. Integers and floats can be worked on by mathematical functions, strings cannot. Notice how when python multiples a number by a integer the expected thing happens. However when a string is multiplied by a integer the string has that many copies of it added i.e. str * 2 = HelloHello. [/QUOTE]
Insert quotes…
Verification
Nawa warak dahaya keeyada? (Namaya wadi kireema dahaya)
Post reply
Top
Bottom